babin raj
babin raj

Reputation: 183

datagridview combobox list color changed to black c#

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        int n = dataGridView1.CurrentCell.RowIndex;
        if (dataGridView1.CurrentCell.ColumnIndex == 0)
        {
            var cbCell = dataGridView1.Rows[n].Cells["category"] as DataGridViewComboBoxCell;
            DataTable dt = c1.ret("select category from category").Tables[0];
            cbCell.DataSource = dt;
            cbCell.ValueMember = "category";
            cbCell.DisplayMember = "category";
            cbCell.FlatStyle = FlatStyle.System;
        }
    }

i am trying to set datasource for datagrid combobox but when i set the datasource. the dropdown list of combobox color turns to black .i have tryed some code for setting the background color but every code failles .now i am stuck with my project. please help me....

Upvotes: 6

Views: 1937

Answers (2)

FSS
FSS

Reputation: 11

So we observed this in the wild and the source seems to be a autoresizing of content in the combobox upon assignment of a new data source. Making it necessary to set the value again and again after each reassigning. The bug can be deactivated by removing autoresize.

Upvotes: 1

ashveli
ashveli

Reputation: 288

You'll have to assign the default color to the combobox control.

After some research I found a solution, thanks to Nick.

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    //your code....
     var cmbBx = e.Control as DataGridViewComboBoxEditingControl; // or your combobox control
     if (cmbBx != null)
     {
         // Fix the black background on the drop down menu
         e.CellStyle.BackColor = this.dgvSummary.DefaultCellStyle.BackColor;
     }
}

Upvotes: 2

Related Questions