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.

Recommended Answers

All 2 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.