datagridviewcomboBox如何在组合框中选择列表的项目
问题描述:
private void comboBoxEx1_SelectedIndexChanged(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
if (comboBoxEx1.SelectedIndex==0)
{
frm2.Show();
}
}
如果comboBox是datagridview而不是Windows窗体,我该如何使此代码工作?
how can i make this code work if comboBox was of datagridview, not of windows form?
答
您的意思是如果selectedindex = 0表示要显示datagridview吗?如果是这样,那么您可以在条件内使用类似以下内容的内容:
Do you mean that you want to show a datagridview if selectedindex = 0? If that''s true, then you can use inside the condition something like:
datagridview1.Visible = true;
只需确保Visible属性的初始状态为false.
[根据评论更新]
因此,您在datagridview中有一个DataGridViewComboBoxColumn,并且想要捕获所选索引是否发生更改.如果是这样,请尝试以下操作.连线datagridview事件:
- EditingControlShowing
- CellEndEdit
- EditingControlShowing
- CellEndEdit
Just make sure that the initial state of the Visible property is false.
[Update based on comment]
So you have a DataGridViewComboBoxColumn in the datagridview and you want to catch if the selected index is changed. If that''s true, try the following. Wire the datagridview events:
// which combobox is wired with a temporary event handler
private ComboBox comboWithEventHandler;
// wire the event handler if edit is started on a comboboxcolumn
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
if (e.Control is ComboBox) {
comboWithEventHandler = ((ComboBox)e.Control);
comboWithEventHandler.SelectedIndexChanged += new EventHandler(temporary_SelectedIndexChanged);
}
}
// fired when the combobox selected index changes
void temporary_SelectedIndexChanged(object sender, EventArgs e) {
MessageBox.Show("Selected index is " + ((ComboBox)sender).SelectedIndex.ToString());
}
// when user moves to another cell detach the event handler
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
if (comboWithEventHandler != null) {
comboWithEventHandler.SelectedIndexChanged -= new EventHandler(temporary_SelectedIndexChanged);
comboWithEventHandler = null;
}
}
无效temporary_SelectedIndexChanged(对象发送方,EventArgs e)
{
如果(comboWithEventHandler.SelectedIndex == 0)
{
//MessageBox.Show("Selected index is" +((ComboBox)sender).SelectedIndex.ToString());
Form2 frm2 =新的Form2();
frm2.Show();
}
}
现在正在工作
thnx非常mika wendelius
void temporary_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboWithEventHandler.SelectedIndex==0)
{
// MessageBox.Show("Selected index is " + ((ComboBox)sender).SelectedIndex.ToString());
Form2 frm2 = new Form2();
frm2.Show();
}
}
now is working
thnx very much mika wendelius