0

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.

GSerg
  • 76,472
  • 17
  • 159
  • 346
PLB
  • 197
  • 1
  • 10
  • 5
    This is not possible. Use if .. else if .. else instead. – Klaus Gütter Nov 18 '19 at 12:42
  • or use the columns name, they should stay fixed, or use an id tag, anything that stays constant. A switch is a dictionary, and the keys of the dictionary are not allowed to change. – Holger Nov 18 '19 at 12:45
  • What is an HMI? – Palle Due Nov 18 '19 at 12:47
  • Thanks for the answer. I'd like to keep the switch case method, because with the if/else if, it will get a bit messy. Maybe there is a way to use an intermediate value which is getting prior ColumnX.Index ? – PLB Nov 18 '19 at 12:51
  • Pall Due: Human Machine Interface – PLB Nov 18 '19 at 13:11

4 Answers4

1

Switches cannot have dynamic evaluated statements in the case statement. They must be statically evaluated.

Below post explain it why :

C# switch statement limitations - why?

Also you can use alternative ways, as explained in below link:

Is there a better alternative than this to 'switch on type'?

CodeGuru
  • 2,722
  • 6
  • 36
  • 52
1

Depending on what you need in your particular case, you could also make use of "Tag" property of the column (it's on DataGridViewBand that is base for DataGridViewColumn). When I worked with forms I used to place there operation code that the button is supposed to trigger. So on the column you place for example Tag="UpdateRecord" and in the code you can have

switch (column.Tag as string)
{
    case "UpdateRecord":
        // Update code
        break;
    // Other cases
}

This way you can easily reorder columns of your grid without playing with fixing those switches. Also (as the next step towards cleaner code) you could put "UpdateRecord" text into const in some class and use it in column definition and switch definition.

0

You'll need to use something else - an else or else if statement could work for you. You could also use an enum of the expected value and reference them in your case statement.

emmademontford
  • 122
  • 1
  • 11
0

I'd like to keep the switch case method

Well, you can abuse pattern matching:

switch (c.ColumnIndex)
{
    case int i when i == Column1.Index:
        // do something
        break;
    case int i when i == Column2.Index:
        // do something
        break;
    //etc
    default: break;
}

but I would not recommend.

GSerg
  • 76,472
  • 17
  • 159
  • 346