3

在 C# 中,我使用枚举并将它们作为字符串添加到列表中。我将列表与 datagridview 绑定。在 datagridview 事件中,我想单击单元格并执行操作。

我将如何使用枚举作为索引而不是行来做到这一点?

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());

在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;
    }
}
4

2 回答 2

1

您可以获取选择的字符串并使用解析将其转换为枚举值,例如:

Colors colorValue = (Colors) Enum.Parse(typeof(Colors), colorString);        

另一个例子:

String WhatDayItIs = DayOfWeek.Monday.ToString();     

DayOfWeek WhatDayItIsDOW;

if (Enum.IsDefined(typeof(DayOfWeek), WhatDayItIs)) 
        WhatDayItIsDOW = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), WhatDayItIs);
于 2012-12-19T13:30:41.587 回答
0

我用

_items = Enum.GetValues(typeof(QSystems)).Cast<QSystems>().ToList();

dataGridView2.DataSource = _items.Select(x => new { Value = x }).ToList();

有了这个开关状态正在唤醒:)

于 2012-12-20T10:05:33.377 回答