如何找出单击哪个DataGridView?

时间:2012-11-25 16:10:50

标签: c# .net winforms

我已经创建了一个DataGridViews数组,并为每个gridview DataGridViewCellEventHandler添加, 我想在单击时更改当前的DataGridView单元格值,要将当前DataGridView索引的单元格值更改为dataGridView [i] .Rows [colIndex] .Cells [rowIndex] .Value,
如何找出点击哪个DataGridView?

这是我的代码。

DataGridView[] altGridViews=new DataGridView[10];
for (int i = 0; i < 10; i++)
{
 altGridViews[i]=new DataGridView();
 altGridViews[i].RowCount = 3;
 altGridViews[i].ColumnCount = 3;
 altGridViews[i].AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
 altGridViews[i].CellClick += new         
 DataGridViewCellEventHandler(altGridView_Click);
 this.Controls.Add(altGridViews[i]);
}

和DataGridView甚至处理程序

protected void altGridView_Click(object sender, DataGridViewCellEventArgs e)
{
     int colIndex = e.ColumnIndex;
     int rowIndex = e.RowIndex;
 //todo: change current cell value
 //altGridViews[i].Rows[colIndex].Cells[rowIndex].Value="something";
} 

1 个答案:

答案 0 :(得分:2)

protected void altGridView_Click(object sender, DataGridViewCellEventArgs e)
{
    int colIndex = e.ColumnIndex;
    int rowIndex = e.RowIndex;
    var currentDataGridView = sender as DataGridView; //your grid
    currentDataGridView.Rows[colIndex].Cells[rowIndex].Value="something";
}