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:
This is how I created the ComboBox column: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
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





Reply With Quote