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 const
s 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.
精彩评论