Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum index for datagridview event

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;
    }
}
like image 928
Alexandros Mor Avatar asked May 02 '26 19:05

Alexandros Mor


1 Answers

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);
like image 84
Steve Wellens Avatar answered May 04 '26 07:05

Steve Wellens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!