3

In C# I am using enums and add them in a list as strings. I bind the list with a datagridview. In datagridview event I want to click cell and do an action.

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

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

2 Answers 2

1

You can take the string that was selected and convert it to the enum value using parse, example:

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);
2
  • So: switch ((QSystems)Enum.Parse(typeof(Qfit4Systems), (string)) and case (string)QSystems.WindowsSystem: you mean something like this? Thank you in advance Dec 19, 2012 at 14:24
  • Yes, something like that. I added another example to my answer. Dec 19, 2012 at 16:25
0

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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.