[英]Enum index for datagridview event
In C# I am using enums and add them in a list as strings. 在C#中,我使用枚举并将它们作为字符串添加到列表中。 I bind the list with a datagridview. 我用datagridview绑定列表。 In datagridview event I want to click cell and do an action. 在datagridview事件中,我想单击单元格并执行操作。
How will I do that with the enum as index and NOT the rows? 如何使用枚举作为索引而不是行?
public enum QSystems { WindowsSystem, systemCheck, QDependencies }
_items = new List<string>();
_items.Add(QSystems.WindowsSystem.ToString());
_items.Add(QSystems.systemCheck.ToString());
_items.Add(QSystems.QDependencies.ToString());
and in datagridview 并在datagridview中
private void dataGridView2_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
_ckecklist = new List<FileStatus>();
switch (e.RowIndex) //this is wrong
{
case (short)QSystems.WindowsSystem:
_ckecklist.Clear();
ShowSystemStatus();
dataGridView1.DataSource = _ckecklist;
SetDatagriDview();
dataGridView1.Show();
break;
case (short)QSystems.systemCheck:
_ckecklist.Clear();
ShowNStatus();
dataGridView1.DataSource = _ckecklist;
SetDatagriDview();
dataGridView1.Show();
break;
case (short)QSystems.QDependencies:
_ckecklist.Clear();
ShowQDependencies();
dataGridView1.DataSource = _ckecklist;
SetDatagriDview();
dataGridView1.Show();
break;
}
}
You can take the string that was selected and convert it to the enum value using parse, example: 您可以使用选择的字符串并使用parse将其转换为枚举值,例如:
Colors colorValue = (Colors) Enum.Parse(typeof(Colors), colorString);
Another example: 另一个例子:
String WhatDayItIs = DayOfWeek.Monday.ToString();
DayOfWeek WhatDayItIsDOW;
if (Enum.IsDefined(typeof(DayOfWeek), WhatDayItIs))
WhatDayItIsDOW = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), WhatDayItIs);
I solve it with 我用它解决了
_items = Enum.GetValues(typeof(QSystems)).Cast<QSystems>().ToList();
and 和
dataGridView2.DataSource = _items.Select(x => new { Value = x }).ToList();
with this the switch state is woking :) 有了这个开关状态是woking :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.