来自DataRow的DataColumn名称(不是DataTable) [英] DataColumn Name from DataRow (not DataTable)
问题描述
我需要遍历特定行的columnname和column数据类型。我看到的所有示例都迭代了整个数据表。我想将单行传递给函数以进行一堆条件处理。我想将条件处理分开以便于阅读。
I need to iterate the columnname and column datatype from a specific row. All of the examples I have seen have iterated an entire datatable. I want to pass a single row to a function to do a bunch of conditional processing. I want to separate the conditional processing for ease of readability.
这就是我所拥有的:
private void doMore(DataRow dr)
{
foreach (DataColumn c in dr.ItemArray) //loop through the columns.
{
MessageBox.Show(c.ColumnName.ToString());
}
}
返回的错误是
System.InvalidCastException:无法将类型为System.String的对象转换为类型System.Data.DataColumn。
System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Data.DataColumn'.
我如何从行中获取列名,还是别无选择,必须将整个数据表传递给函数? / p>
How would I get the column name from the row or do I have no choice and must pass the entire datatable to the function?
推荐答案
您仍然需要遍历 DataTable
类。但是您可以通过使用表
属性。
You would still need to go through the DataTable
class. But you can do so using your DataRow
instance by using the Table
property.
foreach (DataColumn c in dr.Table.Columns) //loop through the columns.
{
MessageBox.Show(c.ColumnName);
}
这篇关于来自DataRow的DataColumn名称(不是DataTable)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!