C# Datagridview How to set value for DataGridViewComboBoxColumn

T.Zacks 3,996 Reputation points
2022-05-14T17:17:29.327+00:00

I have populated my datagridview which has one combobox column.

this way i have populated my combobox column of datagridview.

if (dt != null && dt.Rows.Count > 0)
            {
                DataRow dr = dt.NewRow();
                dr[0] = "0";
                dr[1] = "--Select--";
                dt.Rows.InsertAt(dr, 0);

                (dgListLineitems.Columns["colBM"] as DataGridViewComboBoxColumn).DataSource = dt;
                (dgListLineitems.Columns["colBM"] as DataGridViewComboBoxColumn).DisplayMember = "BM_Element";
                (dgListLineitems.Columns["colBM"] as DataGridViewComboBoxColumn).ValueMember = "Code";

            }

This way i set value for combobox column for each row but no luck

for (int i = 0; i < dgListLineitems.Rows.Count; i++)
            {
                //dgListLineitems.Rows[i].Cells["colBM"].Value = "0";
                dgListLineitems.Rows[i].Cells["colBM"].Value = "--Select--";
            }

i use this dgListLineitems.Rows[i].Cells["colBM"].Value = "0"; but no item is selected in combobox column.

i want to show each combobox in datagridview should have this default text --Select--

FULL Code sample

//Grid bind
dgListLineitems.AutoGenerateColumns = false;
dgListLineitems.DataSource = ListOfElements.Copy();

dgListLineitems.Columns["colRow"].DataPropertyName = "RowNumber";
dgListLineitems.Columns["colLineitem"].DataPropertyName = "LineItem";

dgListLineitems.Columns[0].Width = 60;
dgListLineitems.Columns[1].Width = 75;
dgListLineitems.Columns[2].Width = 498;
dgListLineitems.Columns[3].Width = 502;

//Bind combobox column
if (dt != null && dt.Rows.Count > 0)
{
    DataRow dr = dt.NewRow();
    dr[0] = "0";
    dr[1] = "--Select--";
    dt.Rows.InsertAt(dr, 0);

    (dgListLineitems.Columns["colBM"] as DataGridViewComboBoxColumn).DataSource = dt;
    (dgListLineitems.Columns["colBM"] as DataGridViewComboBoxColumn).DisplayMember = "BM_Element";
    (dgListLineitems.Columns["colBM"] as DataGridViewComboBoxColumn).ValueMember = "Code";
}

//Set value for combobox column
for (int i = 0; i < dgListLineitems.Rows.Count; i++)
{
    dgListLineitems.Rows[i].Cells["colBM"].Value = "0";
}

where i made the mistake. please guide me. Thanks

Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-05-14T23:33:59.953+00:00

    To have a selection for --Select-- you need an entry in the underlying data source. I have a close enough code sample which you can learn from which I change the value of the current row property which is reflected in the DataGridView.

    Each time the Set color button is clicked the underlying code changes the current row color in this case to silver.

    Note the code to do this properly is shown here and no there is not a easier way to do it right. Code is done with .NET Framework 4.8 and will also work with .NET Core.

    201999-changecolor.gif

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.