开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜