//焦点单元格更改时
private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
int col = dataGridView1.CurrentCell.ColumnIndex; //未实例化
switch (col)
{
case 2:
Rectangle rect2 = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, false);
string sexValue2 = dataGridView1.CurrentCell.Value.ToString(); //获取单元格的值
cmb_BuMeng.Text = sexValue2.ToString(); //显示当前更改cmb_BuMeng的Text值
cmb_BuMeng.Left = rect2.Left;
cmb_BuMeng.Top = rect2.Top;
cmb_BuMeng.Width = rect2.Width;
cmb_BuMeng.Height = rect2.Height;
cmb_BuMeng.Visible = true;
break;
case 5:
Rectangle rect5 = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, false);
string sexValue5 = dataGridView1.CurrentCell.Value.ToString(); //获取单元格的值
cmb_Temp.Text = sexValue5.ToString(); //显示当前更改cmb_Temp的Text值
cmb_Temp.Left = rect5.Left;
cmb_Temp.Top = rect5.Top;
cmb_Temp.Width = rect5.Width;
cmb_Temp.Height = rect5.Height;
cmb_Temp.Visible = true;
break;
}
}
dataGridView1 中有两个ComboBox控件 用switch 选择编辑时的列,
dataGridView1.CurrentCell.ColumnIndex 不知怎么实例化
在 DataGrid 控件中 int col=allData.CurrentCell.ColumnNumber; //得到所选单元格的所在列号
是不需要实例化,直接可以赋给 int 变量