C# datagridview 中添加下拉框,并绑定selectedindexchanged事件

时间:2021-08-20 06:50:06

绑定下拉框数据元的链接地址:http://*.com/questions/8711182/datagridivew-datagridviewcomboboxcolumn-select-value-member

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 =newDataGridViewComboBoxColumn(); dgcoSpec =newDataGridViewComboBoxColumn(); 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

绑定selectindexchanged事件的链接地址:http://s.yanghao.org/program/viewdetail.php?i=263596

C# code

//在datagridview的Editing事件里面写privatevoid dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    ComboBox cb = e.Control as ComboBox;
    if (cb !=null)
    {
        cb.SelectedIndexChanged +=new EventHandler(cb_SelectedIndexChanged);
    }
}
void cb_SelectedIndexChanged(object sender, EventArgs e)
{
     //这里执行你需要的,这就是下拉事件}