Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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.
Posted 3-Jan-13 23:48pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Rachna0309 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 574
1 OriginalGriff 373
2 Damith Weerasinghe 355
3 thatraja 350
4 Abhinav S 230
0 Sergey Alexandrovich Kryukov 10,639
1 OriginalGriff 6,658
2 Peter Leow 4,735
3 Abhinav S 3,813
4 Maciej Los 3,565


Advertise | Privacy | Mobile
Web04 | 2.8.140421.2 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid