Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
VB
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.
Posted

If I remember right you need to have a template column and show which object is needed for each row during the datagrid row loading process.

Does this grid have more than one row and can each row have either a textbox or a combobox, or will all the rows have the same thing?

I always showed the combobox and just set the combobox to the value of my data in the column.
 
Share this answer
 
Comments
Rachna0309 4-Jan-13 23:24pm    
yes grid will have more than one row.and each row will have combobox column for a particular column.What i need is if database field for caste is not null,it should display that value in combobox as default value.
Then when you return the value to the datagrid you need to do a set the value by doing a find value within the combobox. I don't have the specific code on hand but if you look for "set a combo box to a specific value" you should find some decent samples.
 
Share this answer
 
VB
Private Sub FillCasteCol()
        Dim con As OdbcConnection = New OdbcConnection
        For Each row As DataGridViewRow In dgvUserDetails.Rows
            If row.Index < dgvUserDetails.Rows.Count - 1 Then
                Dim cmbcell As New DataGridViewComboBoxCell
                dgvUserDetails.Rows(row.Index).Cells(7) = cmbcell
                sql = "Select caste from datafile where Part_No = " & dgvUserDetails.Item("Part_No", row.Index).Value & " and SLNOINPART = " & dgvUserDetails.Item("SLNOINPART", row.Index).Value & " and fullname = '" & dgvUserDetails.Item("Name", row.Index).Value & "'"
                If rs.State = 1 Then rs.Close()
                rs.Open(sql, MainCon, 1, 3)
                If Not rs.EOF And Not IsDBNull(rs.Fields(0).Value) Then
                    gCaste = rs.Fields(0).Value
                    cmbcell.Value = gCaste
                    sql = "Select Description from Category where Catgry = 1 and Description <> '" & gCaste & "'"
                    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")
                    cmbcell.Items.Add(gCaste)
                    cmbcell.DataSource = ds.Tables("Caste")
                    cmbcell.DisplayMember = ds.Tables("Caste").Columns(0).ColumnName.ToString
                    con.Close()
                Else
                    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")
                    cmbcell.DataSource = ds.Tables("Caste")
                    cmbcell.DisplayMember = ds.Tables("Caste").Columns(0).ColumnName.ToString
                End If

            End If
            con.Close()
        Next
    End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900