I am populating datagridview(10 columns) with dataset.I am changing 7th column from textbox to comboboxcell at run time if some value(say caste) in database table for row is null. If caste field is not null in database, then comboboxcell should display that value for that particular row.Below is the code I have tried:
For Each row As DataGridViewRow In dgvUserDetails.Rows
Dim cb As DataGridViewComboBoxCell = New DataGridViewComboBoxCell
If row.Index > 0 Then
If dgvUserDetails.Rows(row.Index).Cells(7).Value.ToString = "" Or Not IsDBNull(dgvUserDetails.Rows(row.Index).Cells(7).Value) Then
Dim con As OdbcConnection = New OdbcConnection
sql = "Select Description from Category where Catgry = 1"
con.ConnectionString = connstring
If con.State = ConnectionState.Open Then con.Close()
con.Open()
Dim da As OdbcDataAdapter = New OdbcDataAdapter(sql, con)
If ds.Tables.Contains("Caste") Then
If ds.Tables("Caste").Rows.Count > 0 Then
ds.Tables("Caste").Rows.Clear()
End If
End If
da.Fill(ds, "Caste")
cb.DataSource = ds.Tables("Caste")
cb.DisplayMember = "Description"
dgvUserDetails.Rows(row.Index).Cells(7) = cb
Else
sql = "Select Description from Category where ID = " & dgvUserDetails.Rows(row.Index).Cells(7).Value.ToString & ""
If rs.State = 1 Then rs.Close()
rs.Open(sql, MainCon, 1, 3)
If Not rs.EOF Then
gCaste = rs.Fields(0).Value
dgvUserDetails.Rows(row.Index).Cells(7).Value = gCaste.ToString
End If
End If
End If
Next
My first record in database table have value for caste field.But debugging this code,after executing this line
dgvUserDetails.Rows(row.Index).Cells(7).Value = gCaste.ToString
debugger goes to dataError event of datagridview.Can any1 let me know wat is the problem in this code?Please help me.its urgent.