mooper
mooper

Reputation: 166

Value doesn't show up on a DataGridViewComboBox control

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];
    }
}

Upvotes: 1

Views: 2251

Answers (4)

Mina Milad
Mina Milad

Reputation: 1

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

Upvotes: 0

Jason Ridge
Jason Ridge

Reputation: 1868

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)

Upvotes: 0

andre
andre

Reputation: 140

    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'.

Upvotes: 1

justAuser
justAuser

Reputation: 60

    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".

Upvotes: 1

Related Questions