I'm a beginner in C# and I'm developing an HMI (WinForms).
I have a DataGridView filled with data and some column with buttons.
When I click on a button of the column, the CellContentClick event is triggering and I would like to get the precise column where the user clicked, using a switch case:
(simplified code)
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0) // if cell button
{
switch (e.ColumnIndex)
{
case Column1.Index: // <- not building because Column1.Index is a variable
// do something
break;
case Column2.Index:
// do something
break;
//etc
default: break;
}
}
}
The problem is that ColumnX.Index
can not be put into the case
condition because it's a variable. Of course, I could use directly the number of the column index, but since I'm developing and always moving the columns, I would like to keep the ColumnX.Index
way.