Switch 中的 C# Datagridview 列名称
当我的 datagridview 触发 dataerror 时,我试图验证我的单元格,等待它们所在的单元格。
这是我的代码:
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 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?*
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要提供一个常量,表示 CurrentCell.OwningColumn.Name 可能的值之一。类似于“另一个值”,而不是对象的属性。
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.
在 C# switch 语句中,case 只能是 'const' 文字、字符串和枚举。它不能是类的属性或字段。
msdn 链接: http://msdn.microsoft.com /en-us/library/06tc147t(v=vs.71).aspx
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
您使用与 switch 检查中相同的属性作为 case 语句。可能您需要将第二种情况替换为
default
关键字。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.我最终创建了我的预期值的枚举并引用它们。这是我可以保证如果发生更改我会收到编译错误的唯一方法。
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.