1,105,334 Community Members

How to get value in comboboxcolumn of datagridview from database

Member Avatar
Rachna0309
Light Poster
36 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have 3 comboboxcolumn in datagridview.I want to display its value in grid if its value in database table is not null.Eg one column is caste.if this is null in my databse table then combobox column should be added to grid else caste value shold be seen in combobox when datagridview loads..Can any1 help me.
Below is my code:

Dim cast As Integer = 0
        Dim con As OdbcConnection = New OdbcConnection
        sql = "Select d.caste from datafile d where d.AC_NO = " & cmbVidhanNo.Text & " " + searchstring
        If rs.State = 1 Then rs.Close()
        rs.Open(sql, MainCon, 1, 3)
        Do While Not rs.EOF
            If Not IsDBNull(rs.Fields(0).Value) Then
                cast = rs.Fields(0).Value
            End If
            If Not IsDBNull(cast) Then
                With col_Caste
                    .Name = "Caste"
                    .HeaderText = "Caste"
                    .DataPropertyName = cast
                    .ReadOnly = False
                End With

            Else
                sql = "Select Desc 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)
                da.Fill(ds, "Caste")

                With col_Caste
                    .Name = "Caste"
                    .HeaderText = "Caste"
                    .DataSource = ds.Tables("Caste")
                    .DisplayMember = "Desc"
                    .ReadOnly = False
                End With

            End If
            rs.MoveNext()
        Loop

        dgvUserDetails.Columns.Add(col_Caste)
Member Avatar
adam_k
Veteran Poster
1,056 posts since Jun 2011
Reputation Points: 239 [?]
Q&As Helped to Solve: 212 [?]
Skill Endorsements: 17 [?]
 
0
 

To add a comboboxcolumn in a datagridview use the following:

'set up the combobox properties : 
Dim column As New DataGridViewComboBoxColumn
column.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox
column.Name = "Name"
column.HeaderText = "Title"
column.DataPropertyName = "Database column here" 
column.DataSource = "A binding source goes here" 
column.ValueMember = "Value member name here"
column.DisplayMember = "Display member name here" 
column.FlatStyle = FlatStyle.Flat
column.DisplayStyle = style
' and finally add it to your dgv
dgv.columns.add(column)
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article