Results 1 to 11 of 11

Thread: [RESOLVED] [2005] How to programmatically assign a value in a datagridviewcomboboxcell

Threaded View

  1. #1

    Thread Starter
    Fanatic Member eimroda's Avatar
    Join Date
    Jul 2000
    Location
    Philippines
    Posts
    642

    Resolved [RESOLVED] [2005] How to programmatically assign a value in a datagridviewcomboboxcell

    I have an unbound DataGridView and a ComboBox cell in it. I got a problem when assigning a value to the combobox cell at runtime. below is my code:
    Code:
        Private Sub GetRecords(ByVal strSQL As String)
            Try
                If IsConnected() Then
                    Dim myCommand As SqlCommand = New SqlCommand(strSQL, Conn)
                    Dim myDataReader As SqlDataReader = myCommand.ExecuteReader()
    
                    Dim strID, strLName, strFName, strMI, strContactNo, strAddress, strBIR, strPHIC As String
                    Dim intSpecialization As Integer
                    Dim strRow As String()
    
                    Grid.RowCount = IIf(Grid.AllowUserToAddRows, 1, 0)
    
                    With myDataReader
                        Do While myDataReader.Read()
                            strID = IIf(.IsDBNull(0), 0, .GetInt32(0).ToString)
                            strLName = IIf(.IsDBNull(1), "", .GetString(1).Trim)
                            strFName = IIf(.IsDBNull(2), "", .GetString(2).Trim)
                            strMI = IIf(.IsDBNull(3), "", .GetString(3).Trim)
                            intSpecialization = IIf(.IsDBNull(4), 1, .GetInt32(4))
                            strContactNo = IIf(.IsDBNull(5), "", .GetString(5).Trim)
                            strAddress = IIf(.IsDBNull(6), "", .GetString(6).Trim)
                            strBIR = IIf(.IsDBNull(7), "", .GetString(7).Trim)
                            strPHIC = IIf(.IsDBNull(8), "", .GetString(8).Trim)
    
                            'error in this line:
                            'System.FormatException: DataGridViewComboBoxCell value is not valid
                            'the intSpecialization variable is the value for the combo box column
                            strRow = New String() {strID, strLName, strFName, strMI, intSpecialization, _
                                strContactNo, strAddress, strBIR, strPHIC}
    
                            Grid.Rows.Add(strRow)
                        Loop
                    End With
    
                    'close data reader
                    If myDataReader IsNot Nothing Then myDataReader.Close()
                    LastSqlUsed = strSQL
                    GridChanged = False
                    CloseConnection()
                End If
            Catch ex As Exception
                DisplayError(ex.Message)
            End Try
        End Sub
    This is how I created the ComboBox column:
    Code:
        Sub FillCombo()
            Dim myColumn As New DataGridViewComboBoxColumn
    
            If IsConnected() Then
                Dim sqlCommand As New SqlCommand("SELECT * FROM tblSpecializations ORDER BY Sp_Desc", Conn)
                Dim sqlAdapter As New SqlDataAdapter
                sqlAdapter.SelectCommand = sqlCommand
                Dim myTable As New DataTable
    
                sqlAdapter.Fill(myTable)
    
                With myColumn
                    '.DataPropertyName = "colSpecialization"
                    .Name = "colSpecialization"
                    .HeaderText = "Specialization"
                    .DropDownWidth = 160
                    .Width = 200
                    .MaxDropDownItems = 10
                    '.FlatStyle = FlatStyle.Popup
                    'set the data properties
                    .DataSource = myTable
                    .ValueMember = "Sp_Control"
                    .DisplayMember = "Sp_Desc"
                End With
                Grid.Columns.Remove("colSpecialization")
                Grid.Columns.Insert(4, myColumn)
            End If
    
        End Sub
    Last edited by eimroda; Aug 16th, 2007 at 08:01 PM.
    On Error GoTo Hell

    Hell:
    Kill Me


    Food For Thought:

    - Do not judge a book... if you're not a judge!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width