Hi,
My application is in VS2008 coded in Vb.net
I have a datagridView on one of my form.This is filled from the database at the load event of the form.The datagridView also has a column of type DatagridViewComboboxcolumn.
This DatagridViewComboboxcolumn already has 4 values added to it in its collection during design time.My requirement is when the data is loaded in the datagridview, the corresponding record which is supposed to be loaded in DatagridViewComboboxcolumn should compare its value with the value of DatagridViewComboboxcolumn.And if the value matches than that value should get selected in DatagridViewComboboxcolumn.
The database will have the any of the 4 values that are already in DatagridViewComboboxcolumn.It just has to compare both values and select the matching value.
Below is my Code

query= "Select Record1,Record2,Record3 from TableName"
        cmd = New SqlCommand(query, connection)
        dr = cmd.ExecuteReader()
        If dr.HasRows Then
            While dr.Read()
                datagridView.rows.add(dr(0),dr(1),dr(2))
                              
            End While
        End If

dr(2) is supposed to compare the record with DatagridViewComboboxcolumn.
How can i compare it with the value of my DatagridViewComboboxcolumn of my datagridView
Please suggest.

Dani AI

Generated

reported filling the grid row-by-row from a reader while a design-time DataGridViewComboBoxColumn already contains four items; suggested a DataAdapter/DataSet bind but saw an extra column and no automatic selection. The behavior stems from two common mismatches: mixing bound and unbound modes (which creates duplicate auto-generated columns) and value/type mismatches between the DB value and the combo column's items or ValueMember.

Two reliable approaches

  1. Bound grid (recommended when loading a table)
  • Turn off automatic column generation so design-time columns are used:
    • set DataGridView.AutoGenerateColumns = False.
  • Give the combo column a DataPropertyName that matches the DB column.
  • If the combo should show lookup names, bind the combo column to a lookup table and set DisplayMember and ValueMember. The grid will store the ValueMember in the cell and show the DisplayMember.
  • If the combo contains static strings (design-time Items), the DB value must match exactly (same type, trimmed, same case if relevant).

Example (VB):

DataGridView1.AutoGenerateColumns = False
Dim c As DataGridViewComboBoxColumn = CType(DataGridView1.Columns("ComboCol"), DataGridViewComboBoxColumn)
c.DataSource = lookupTable        ' DataTable with Id/Name
c.DisplayMember = "Name"
c.ValueMember = "Id"
c.DataPropertyName = "LookupId"   ' column in main data table
DataGridView1.DataSource = mainTable
  1. Unbound/manual rows (the pattern @EhteshamSiddiq started)
  • Do not set DataSource on the grid. Add rows manually and set the combo cell value to the database value (convert/trim as needed).

Example (VB):

Dim r = DataGridView1.Rows.Add()
DataGridView1.Rows(r).Cells("ComboCol").Value = reader("ComboField").ToString().Trim()

Troubleshooting checklist

  • Ensure AutoGenerateColumns = False when using design-time columns.
  • Confirm DataPropertyName is correct.
  • Verify types: ValueMember type must match DB column type (ints vs strings).
  • Trim whitespace and normalize case if necessary.
  • Handle DataGridView.DataError to log mismatches and avoid runtime exceptions.

Applying one of the two clean patterns above will prevent duplicate columns and let the combobox cell select the correct value automatically.

Recommended Answers

All 2 Replies

Hi.

Here are some alternate steps on how you can do it.
1. databind your datagridview in design mode
2. edit the desired column to make it a combobox column and add the items
3. then on the formload you can try this code :

Dim ds as new Dataset
Dim da as new sqlclient.SqlDataAdapter
da.SelectCommand = new Sqlclient.SqlCommand("Select Record1,Record2,Record3 from TableName",connection)
da.Fill(ds,"TableName")

datagridview1.datasource = ds
datagridview1.datamember = "TableName"

that should fill the table and select the correct value from the combobox column.
so this is a way to go using data sets and data adapters.

hope it helps, or gave you an idea on how to tackle the issue.

hi,
i have tried that.
I first designed my datagridview and set one of the column in datagridview as DatagridViewComboboxcolumn.When im using Dataadapter method to fill my datagridview it is filling 4 columns.3 columns that are fetched from database and the one extra column that i designed in datagridview during design time.Also its not selecting value according to my requirement.
Where am i goin wrong ?Any suggestions.

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.