Question

i have datagridview which has one combobox column. i populate combobox column. when i select any text from combobox column then i need to get the value when i read the data in for loop.

dgFilter is here datagridview

            DataGridViewComboBoxColumn dgcoSpec = new DataGridViewComboBoxColumn();
            dgcoSpec = new DataGridViewComboBoxColumn();
            dgcoSpec.DataSource = loadOperators();
            dgcoSpec.DisplayMember = "Operatortxt";
            dgcoSpec.ValueMember = "Operatorvalue";
            dgcoSpec.AutoComplete = true;
            dgcoSpec.HeaderText = "Operators";
            dgcoSpec.Name = "Operators";
            dgcoSpec.DefaultCellStyle.NullValue = "--Select--";
            dgcoSpec.Width = 130;
            dgFilter.Columns.Insert(1, dgcoSpec);

here this way i read data from combobox column

for (int i = 0; i <= dgFilter.Rows.Count - 1; i++)
{
  strOperator = dgFilter.Rows[i].Cells[1].Value.ToString().Trim();
}

but the problem is i am not getting the combox value member rather i am getting display member. so how to extract value member from for loop. please guide me with code. thanks

Was it helpful?

Solution

The value member is the string that is displayed inside the DataGridViewComboboxCell.

The actual Combobox control only exists during the time frame in which the user is editing the cell.

If you mean that you would like to get the index of the value in the list of the DataGridViewComboboxCell items, you can query the index of the value:

for (int i = 0; i <= dgFilter.Rows.Count - 1; i++)
{
    var cell = dgFilter.Rows[i].Cells[1] as DataGridViewComboboxCell;
    int index = cell == null || cell.Value == null ? -1 : cell.Items.IndexOf(cell.Value);
    Console.WriteLine(index);
}

In this example, I am using -1 as an invalid value.

EDIT Just noticed you are using a DataSource; See DataGridViewComboBoxColumn name/value how? for possible duplicate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top