【问题标题】:How to find out which DataGridView is clicked?如何找出点击了哪个 DataGridView?
【发布时间】:2012-11-13 05:15:08
【问题描述】:

我已经动态地创建了一个 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";
} 

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:
    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";
    } 
    

    【讨论】:

    • 这正是我所需要的!非常感谢!
    • 这对我有帮助。对我来说,我必须将最后一行中的索引切换为: currentDataGridView.Rows[rowIndex].Cells[colIndex].Value = "something";
    猜你喜欢
    • 1970-01-01
    • 2011-11-22
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多