简体   繁体   English

Switch中的C#Datagridview列名

[英]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.我正在尝试验证我的单元格,当数据错误为我的 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...?我认为此时 owningcolumn 名称是不变的......?

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.您需要提供一个常量,表示 CurrentCell.OwningColumn.Name可能是的值之一。 Something like "anothervalue", and not a property of an object.类似于“另一个值”,而不是对象的属性。

In c# switch statement's cases can only be 'const' literals , strings and enums.在 c# switch 语句的情况下只能是“const”文字、字符串和枚举。 It cannot be property or field of class.它不能是类的属性或字段。 msdn link: http://msdn.microsoft.com/en-us/library/06tc147t(v=vs.71).aspx msdn 链接: 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.您正在使用与 switch 检查中相同的属性作为 case 语句。 Probably you need to replace second case with default keyword.可能您需要用default关键字替换第二种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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