2012-11-25 8 views
0

J'ai créé un tableau de DataGridViews dinamiquement, et ajouté pour chaque gridview DataGridViewCellEventHandler, je veux changer la valeur actuelle de la cellule DataGridView quand on clique dessus, pour changer la valeur de la cellule que vous devez connaître l'index DataGridView actuel du tableau comme dataGridView [i ] .Rows [colIndex] .Cells [rowIndex] .Value,
comment savoir quel DataGridView est cliqué?Comment savoir quel DataGridView est cliqué?

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

et DataGridView même 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"; 
} 

Répondre

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

C'est exactement ce qu'il me fallait! Merci beaucoup! –

+0

Cela m'a aidé. Pour moi, j'ai dû changer les indices dans la dernière ligne à: currentDataGridView.Rows [rowIndex] .Cells [colIndex] .Value = "quelque chose"; – peitek

Questions connexes