简体   繁体   中英

C# Datagridview Column Name in Switch

I am trying to validate my cells pending on what cell they are in when the dataerror fires for my datagridview.

Here is my code:

private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
  if (_licDataSet.LicenseFileTable.Rows(e.RowIndex).Columns(e.ColumnIndex) == "test")

    switch (dataGridView1.CurrentCell.OwningColumn.Name)
    {
      case "AllowAsRemoteDesktopColumn" :
        // do not think there are any checks for this column
        // we'll find out shortly though!
        break;
      case dataGridView1.CurrentCell.OwningColumn.Name : 
        // ^^^ this errors with "A constant value is expected"
        // do something
        break;
    }
}

I am erroring at the ^^^ position as you can see from the message.

What am I doing wrong? I would think that the owningcolumn name was constant at this point...?

Help!

*** EDIT*** How do I get the column name of the current cell is my question if the above doesn't work? *

I ended up creating an enum of my expected value and referencing them. It was the only way I could guarantee if the changed that I'd get a compile error.

You need to provide a constant representing one of the values the CurrentCell.OwningColumn.Name could be. Something like "anothervalue", and not a property of an object.

In c# switch statement's cases can only be 'const' literals , strings and enums. It cannot be property or field of class. msdn link: http://msdn.microsoft.com/en-us/library/06tc147t(v=vs.71).aspx

You are using as a case statement the same property as in a switch check. Probably you need to replace second case with default keyword.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM