开发者

How may I address a particular cell of a DataGridView row in a compiler-checked fashion?

开发者 https://www.devze.com 2023-04-06 04:02 出处:网络
For example like this: myDataGridView.Rows[0].<something>myColumnName</something>. Currently I am using

For example like this:

myDataGridView.Rows[0].<something>myColumnName</something>.

Currently I am using

myDataGridView.Rows[0].Cells["myColumnName"]

but this is not compiler-checke开发者_JAVA百科d.

[deleted]

Thanks.


Perhaps you want:

myDataGridView.Rows[0].Cells[RevParticlesID.Index]

for:

myDataGridView.Rows[0].Cells["myColumnName"] 


There's no way to have the compiler ensure you're using actual column names. But you can get close using consts or an enum.

struct ColumnNames
{
    public const string myFirstColumn = "myFirstColumn";
    public const string mySecondColumn = "mySecondColumn";
    public const string myOtherColumn = "myOtherColumn";
}

myDataGridView.Rows[0].Cells[ColumnNames.myFirstColumn]

or

enum ColumnNames
{
    myFirstColumn,
    mySecondColumn,  
    myOtherColumn
}

myDataGridView.Rows[0].Cells[ColumnNames.myFirstColumn.ToString()]

But it's up to you to make sure you use the ColumnNames values as column names.

0

精彩评论

暂无评论...
验证码 换一张
取 消