问题描述
我在我的DataGridView中创建了comboBox列
as
DataGridViewCell ComboCell = new DataGridViewComboBoxCell();
DataGridViewComboBoxColumn comboCol = new DataGridViewComboBoxColumn( );
comboCol.AutoComplete = true;
comboCol。 CellTemplate = ComboCell;
comboCol.DisplayMember =MenuName;
comboCol.ValueMember =MenuID;
comboCol.HeaderText =菜单名称;
comboCol.DataSource = dealList;
comboCol.Name =MenuName;
comboCol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter ;
comboCol.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
comboCol.Width = 120;
Grd.Columns.Add(comboCol );
现在我想如果组合框中的索引在此列中更改说MenuName然后我可以将所选值分配给另一列说MenuID
我怎么能这样做?
请指导我...提前谢谢你
I have created comboBox column in my DataGridView
as
DataGridViewCell ComboCell = new DataGridViewComboBoxCell();
DataGridViewComboBoxColumn comboCol = new DataGridViewComboBoxColumn();
comboCol.AutoComplete = true;
comboCol.CellTemplate = ComboCell;
comboCol.DisplayMember = "MenuName";
comboCol.ValueMember = "MenuID";
comboCol.HeaderText = "Menu Name";
comboCol.DataSource = dealList;
comboCol.Name = "MenuName";
comboCol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
comboCol.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
comboCol.Width = 120;
Grd.Columns.Add(comboCol);
Now i want if the index in the combobox is changed in this column say "MenuName" then i can assign the selected value to another column say "MenuID"
how can i do this?
Please guide me... Thank you in advance
推荐答案
private void dataGridViewSales_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb = e.Control as ComboBox;
if (cb != null)
{
cb.SelectedIndexChanged += new EventHandler(selectionchange);
//here SelectedIndexChanged event is used
}
}
然后定义selectionchange方法
and then define selectionchange method
void selectionchange(object sender, EventArgs e)
{
try
{
ComboBox cb = (ComboBox)sender;
if (cb.Text == "yes")
{
dataGridViewSales.CurrentRow.Cells[1].Value= "Hello";
//if datagridviewcombobox text is yes then set the first column value to 'Hello'
}
else if (cb.Text == "no")
{
dataGridViewSales.CurrentRow.Cells[1].Value = string.Empty;
//else set the first column value to empty
}
}
catch { }
}
谢谢
thanks
这篇关于在DataGridView中使用DataGridViewComboBoxColumn及其值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!