问题描述
我有一些code这列名即设置一个DataRow单元格的值。
I have some code which sets the value of cells in a DataRow by column name i.e.
row["ColumnName"] = someValue;
我想也立即设置此行的值列于一个上面的权利。显然,如果我是越来越被索引,而不是按列名的单元格,这将是很容易。那么,有没有从列名获取列索引从而使我做的一种方式:
I want to also set the value for this row in the column immediately to the right of the one found above. Clearly if I was getting the cell by index rather than by column name this would be easy. So is there a way of getting the column index from the column name thus allowing me to do:
row[index + 1] = someOtherValue;
即。我需要创建一些列的索引和列名的字典最初创建表时,或者我可以从列名的索引后来没有这样做呢?
i.e. do I need create some kind of dictionary of column index and column names when the table is initially created, or can I get the index from the column name later on without doing this?
推荐答案
您可以使用的获得在 DataTable中的列的索引
。所以,如果你需要在下列提到,在使用 Column.Ordinal + 1
:
You can use DataColumn.Ordinal
to get the index of the column in the DataTable
. So if you need the next column as mentioned use Column.Ordinal + 1
:
row[row.Table.Columns["ColumnName"].Ordinal + 1] = someOtherValue;
这篇关于获得与名称的DataTable列的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!