Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use Enums instead of column index in C#

i am trying to use Enum as a Datagridview Coloumn index, but failed

My Enums

enum _EnmDgvCol
    {
        SrNo,
        Name,
        Qty,
        Rate,
        Amount,
    }

and I am trying to as follows:

MydataGridView[_EnmDgvCol.Name, 0].Value = "Some Value";

but its giving Error

The best overloaded method match for 'System.Windows.Forms.DataGridView.this[int, int]' has some invalid arguments

if i use following code its works

MydataGridView[1, 0].Value = "Some Value";

My Question is How to Use Enums instead of column index

like image 469
subhash bhule Avatar asked Oct 14 '25 03:10

subhash bhule


1 Answers

Type cast the enum value to int for instance you have _EnmDgvColInstance that has value as _EnmDgvCol.Name

_EnmDgvCol _EnmDgvColInstance = _EnmDgvCol.Name;
MydataGridView[(int)_EnmDgvColInstance, 0].Value = "Some Value";

Or simply

MydataGridView[(int)_EnmDgvCol.Name, 0].Value = "Some Value";
like image 141
Adil Avatar answered Oct 18 '25 01:10

Adil



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!