Aydar Omurbekov
Aydar Omurbekov

Reputation: 2117

How to find out which DataGridView is clicked?

I've created an array of DataGridViews dinamically, and added for each gridview DataGridViewCellEventHandler, i want to change current DataGridView cell value when clicked, to change the cell value you have to know current DataGridView index from the array as dataGridView[i].Rows[colIndex].Cells[rowIndex].Value,
how to find out which DataGridView is clicked?

This is my code.

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]);
}

and DataGridView even handler

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";
} 

Upvotes: 0

Views: 612

Answers (1)

RavingDev
RavingDev

Reputation: 2967

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";
} 

Upvotes: 2

Related Questions