Search code examples
c#winformsdatagridviewdatagridviewcombobox

How to change the text of a comboBox cell of a dynamically added DataGridViewComboBoxColumn?


I am still a little new to C# but I am using Winforms and I have a DataGridView which is connected to a datasource and is being populated correctly.

I have also added a ComboBoxColumn at run time. This ComboBoxColumn is being connected to a datasource, the displaymember and valuemember are set, the headertext is set and then the column is added to the datagrid. The connection works just fine and when the user clicks the comboBox is populated as expected.

When a user completes a new row in the grid he will obviously select the item in the comboBox and then the whole row is updated to my database when done.

My question is: When this datagrid is opened again or redrawn it is populated due to the datasource property, but how do I get the comboBox cell to display the VALUE he selected originally instead of just a blank box. I know the code of getting the value from the DB and put in a value. The ideal would be if I could put that variable in the display of the comboBox. Keep in mind that the comboBox is still data bound so that the user could edit the value if he so wishes?

I know that in a normal comboBox control I should just set the .Text property. But the DataGridViewComboBox does not have the same property.

Here is the code for the actual data connection and adding of the comboBoxColumn:

    public void AddComboBoxColumn(DataGridView datagridName, DataTable table, string headerText, int columnIndex)
    {
        DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();

        GetDisplayAndValueMembers(table, headerText); //Calls method that gets the datasource, displaymember, valuemember depending on column

        column.DataSource = tableRef; //sets datasource to table referenced by column
        column.DisplayMember = displayMember; //sets displaymember
        column.ValueMember = valueMember; //sets valuemember

        column.HeaderText = headerText; //changes headertext to displayed text

        if (newColumnIndex == 0)
            datagridName.Columns.Add(column); //added to end of datagrid
        else
        {
            datagridName.Columns.RemoveAt(columnIndex);
            datagridName.Columns.Insert(newColumnIndex, column); //added in specific index if needed
        }
    }

This just shows the dataconnection of the drop down. Obviously there are many methods being used with a large amount of code. But this is not the problem as this works fine. I don't know how to go about the issue of actually showing a previously selected item as the text of the comboBox?


Solution

  • It sounds like what you are looking for is the DataPropertyName property of the ComboBoxColumn. This property creates the binding between the DataSource of the DataGridView and the selected value of the ComboBoxColumn.

    For example, say you have a list of Products displayed in the combo boxes. You would then also have a ProductId in your DataGridView DataSource. Something like this:

    // There Orders is a data table coming from the db which includes the product id column 
    dataGridView1.DataSource = Orders;
    
    // You set up your column just the same, with the DisplayMember and ValueMember
    DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();         
    GetDisplayAndValueMembers(table, headerText);
    column.DataSource = tableRef; //sets datasource to table referenced by column
    column.DisplayMember = displayMember; //sets displaymember
    column.ValueMember = valueMember; //sets valuemember       
    column.HeaderText = headerText; //changes headertext to displayed text   
    
    //Now you also set the DataPropertyName
    column.DataPropertyName = "ProductId"; // this is the name of a column or property from the grid datasource
    
    dataGridView1.Columns.Add(column);
    

    With the DataPropertyName set you now will have databinding between the ComboBoxColumn and the DataGridView.

    If you underlying data source absolutely cannot have the value of the combo box property in it then you will need to handle all of this saving and value setting of the ComboBoxColumn within custom code.

    To set the selected value of a DataGridViewComboBoxCell all you do is select the cell and then set its Value property.

    dataGridView1.Rows["rowname"].Cells["columnname"].Value = valueFromDb;