Value doesn't show up on a DataGridViewComboBox control

2.2k Views Asked by At

After setting the values in the DataGridView, they don't show up on the control even though I can access them through the Value property.

The problem is that there is no default Value in the ComboBox cells.

void ComboBoxColumn()
{
    string[] values =  { "one", "two", "three" };
    string columnName = "Test";
    var column = new DataGridViewComboBoxColumn();
    column.Name = columnName;
    column.ValueType = typeof(string);

    foreach(string item in values)
    {
        column.Items.Add(item);
    }

    Grid.Columns.Add(column);

    // problematic part
    foreach(DataGridViewRow row in Grid.Rows)
    {
        row.Cells[columnName].Value = values[0];
    }
}
4

There are 4 best solutions below

0
On

I cant reproduce your error. Here is my sample project

Try and give us anything else that might be the issue.

note: I copied your code directly (and then added in some rows)

4
On
    void SetValues()
    {
        string[] values = { "one", "two", "three" };
        string columnName = "Test";
        var column = new DataGridViewComboBoxColumn();
        column.Name = columnName;
        column.ValueType = typeof(string);

        foreach (string item in values)
        {
            column.Items.Add(item);
        }

        dataGridView1.Columns.Add(column);
    }

On my machine this code is running. If I click the ComboBoxColumn I can see the tree values and choose one. What is your problem?

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            row.Cells[columnName].Value = values[1];
        }

If you add this code, the default value of the combo box shows the value 'two'.

5
On
    void AddComboColumn()
    {
        string[] values = { "one", "two", "three" };
        string columnName = "Test";
        var column = new DataGridViewComboBoxColumn();
        column.Name = columnName;
        column.ValueType = typeof(string);

        foreach (string item in values)
        {
            column.Items.Add(item);
        }

        dataGridView1.Columns.Add(column);

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            row.Cells[columnName].Value = values[2];
        }
    }

These code work too... Check, may be problem in 'RandomValue(values);'? I haven't this part of code and can't tell for sure. P.S. In my prog default value == "three".

0
On

First Make name for combo box in datagridveiw ColDryg then in initvalue function write this code

  DataGridViewComboBoxColumn ColDryg =
 (DataGridViewComboBoxColumn)Gv_dtails.Columns["ColDryg"];
             ColDryg.DataSource = db.Drugs.ToList();
             ColDryg.ValueMember = "Id";
             ColDryg.DisplayMember ="Arabic_name";

you have table in database its name is Drug