question

TZacks-2728 avatar image
0 Votes"
TZacks-2728 asked karenpayneoregon answered

C# Datagridview How to set value for DataGridViewComboBoxColumn

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




dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

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



changecolor.gif (158.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.