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()
                    Dim da As OdbcDataAdapter = New OdbcDataAdapter(sql, con)
                    If ds.Tables.Contains("Caste") Then
                        If ds.Tables("Caste").Rows.Count > 0 Then
                        End If
                    End If

                    da.Fill(ds, "Caste")
                    cb.DataSource = ds.Tables("Caste")
                    cb.DisplayMember = "Description"
                    dgvUserDetails.Rows(row.Index).Cells(7) = cb

                    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

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.

4 Years
Discussion Span
Last Post by Rachna0309

i'm not really understand what you want, sorry my bad english, here is what i got from your explanation : if caste field in database = null, then 7th column in datagridview should textboxcell type. Else if caste field in database not null, then 7th column in datagridview should comboboxcell type. am i right?


Sorry I didnt updated my post.What I need is all cells in caste column should be comboboxcell type.But if caste field in database is not null,then that cell should show the value that is in database.
For eg. my first record has caste value as hindu.hindu should be default value in comboboxcell for that particular row.

This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.