日期:2014-05-17 浏览次数:20931 次
private void dataGridView1_EditingControlShowing(objectsender,DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.RowIndex != -1 &&dataGridView1.CurrentCell.ColumnIndex == 0)
{
//还原当前控件并绑定事件
((ComboBox)e.Control).SelectedIndexChanged += new EventHandler(Form1_SelectedIndexChanged);
}
}
//事件名自己定义
private void Form1_SelectedIndexChanged(object sender, EventArgs e)
{
//事件代码
MessageBox.Show(((ComboBox)sender).SelectedIndex.ToString());
}
private DataGridView dataGridView1 = new DataGridView();
private void AddColorColumn()
{
DataGridViewComboBoxColumn comboBoxColumn =
new DataGridViewComboBoxColumn();
comboBoxColumn.Items.AddRange(
Color.Red, Color.Yellow, Color.Green, Color.Blue);
comboBoxColumn.ValueType = typeof(Color);
dataGridView1.Columns.Add(comboBoxColumn);
dataGridView1.EditingControlShowing +=
new DataGridViewEditingControlShowingEventHandler(
dataGridView1_EditingControlShowing);
}
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
// Remove an existing event-handler, if present, to avoid
// adding multiple handlers when the editing control is reused.
combo.SelectedIndexChanged -=
new EventHandler(ComboBox_Selec