Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridViewComboBox DataSource

Tags:

c#

winforms

I currently have a 2-column wide DataGridView, the first column being a DataGridViewTextBoxColumn and the second a DataGridViewComboBoxColumn. I also have a pre-generated generic List (string) that is to be used as the DataSource for the DataGridViewComboBox for each row.

Finally, I have a loop that iterates through a series of strings and parses them accordingly, with extracted values being applied to respective cells using an as shown below:

dataGridView.Rows.Add("Column1Text", "Column2Text");

The gridview data is filled as expected, along with the DataGridViewComboBox properly displaying the ideal item.

The problem is, the DataGridViewComboBox, when clicked, does not drop down any items. I have checked that the DataGridViewComboBox contains items. The DataGridViewTextBoxColumn's AutoSizeMode is set to "Fill" if it's of any relevance.

Any insight as to what I may be doing wrong? Do I have to manually drop down the items when a given cell is clicked? Thanks.

Update

I have tried two different methods in terms of binding the generic list as the DataSource.

The first was binding the DataSource of the entire column itself via:

col_key.DataSource = KeyList;

The second method was binding the DataSource of each new DataGridViewComboBoxCell in the corresponding row:

(DataGridViewComboBoxCell)(row.Cells[1]).DataSource = KeyList;

Both of these methods compile and properly add the necessary items at runtime, but no items drop down when clicked.

like image 267
user Avatar asked Jan 26 '26 13:01

user


1 Answers

I chose to handle this in the CellEnter event:

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 2)
        {
            DataGridViewComboBoxCell cell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewComboBoxCell;
            if (cell.DataSource == null)
            {
                cell.DataSource = this._ComboItemsBindingSource;
                cell.DisplayMember = "Value"; //lite-weight wrapper on string
                cell.ValueMember = "Value";   //where Value is a property
            }
        }
    }
like image 116
Marvin Winks Avatar answered Jan 28 '26 04:01

Marvin Winks