获得与名称的DataTable列的索引 [英] get index of DataTable column with name

查看:333
本文介绍了获得与名称的DataTable列的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些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?

推荐答案

您可以使用的 DataColumn.Ordinal 获得在 DataTable中的列的索引 。所以,如果你需要在下列提到,在使用 Col​​umn.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列的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆