遍历数据表以从每一行获取两个特定值 [英] looping through data table to get two specifics values from each row

查看:58
本文介绍了遍历数据表以从每一行获取两个特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图遍历数据表并打印出


  • 的值0,

  • 1和

  • 每行第4个单元格,

感觉这应该可行,但我可能以某种方式弄乱了循环。

And it feels like this should be working but I'm probably messing up the loop somehow.

 foreach (DataRow dr in dt.Rows)
 {
      for (int x = 0; x < dt.Rows.Count; x++)
      {
          Console.WriteLine(dt.Columns[0].ColumnName + " ");
          Console.WriteLine(dt.Rows[x].ItemArray[0].ToString() + " ");
          Console.WriteLine(dt.Columns[1].ColumnName + " ");
          Console.WriteLine(dt.Rows[x].ItemArray[1].ToString() + " ");
          Console.WriteLine(dt.Columns[4].ColumnName + " ");
          Console.WriteLine(dt.Rows[x].ItemArray[4].ToString() + " ");
       }
 }

上面的代码给我这个错误:

The above code gives me this error:


系统无法执行指定的程序。

The system cannot execute the specified program.


推荐答案

您不需要在行上用 foreach 循环将 for 循环包围起来。 (您根本没有使用 dr

You don't need to surround your for loop with a foreach loop on the Rows. (You are not using the dr at all)

for (int idx = 0; idx < dt.Rows.Count; idx++)
{
    Console.WriteLine(dt.Columns[0].ColumnName + " ");
    Console.WriteLine(dt.Rows[idx].ItemArray[0] + " ");
    Console.WriteLine(dt.Columns[1].ColumnName + " ");
    Console.WriteLine(dt.Rows[idx].ItemArray[1] + " ");
    Console.WriteLine(dt.Columns[4].ColumnName + " ");
    Console.WriteLine(dt.Rows[idx].ItemArray[4] + " ");
}

更通用的版本:

int[] columnIndexes = new[] { 0, 1, 4 };
for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
{
    for (int columnIndex = 0; columnIndex < columnIndexes.Length; columnIndex++)
    {
        Console.WriteLine(dt.Columns[columnIndex].ColumnName + " ");
        Console.WriteLine(dt.Rows[rowIndex].ItemArray[columnIndex] + " ");
    }
}




如果要遍历这样,您可以使用foreach进行行集合,但要复杂一些。


If you want to iterate through the Rows collection with foreach then you can do but it is a bit more trickier.

DataTable的Rows属性是 DataRowCollection 。它公开了 GetEnumerator 方法,这对于 foreach 循环必不可少。

DataTable's Rows property is a DataRowCollection. It exposes a GetEnumerator method, which is essential for the foreach loop.

foreach (DataRow dr in dt.Rows)
{
    //dr does not provide you direct access to the ColumnName
}

您不能直接从DataRow访问ColumnName。所有需要做的就是创建一个查找表。

You can't access the ColumnName from the DataRow directly. All need to do is to create a "lookup table" for the column names where the key is the index and the value is the name of the column.

int colIdx = 0;
var columnNames = dt.Columns
    .Cast<DataColumn>()
    .ToDictionary(_ => colIdx++, column => column.ColumnName);

之后,您的foreach循环将如下所示:

After that your foreach loop would look like this:

int[] columnIndexes = new[] {0, 1, 4};
foreach (DataRow row in dt.Rows)
{
    for (int columnIndex = 0; columnIndex < columnIndexes.Length; columnIndex++)
    {
        Console.WriteLine(columnNames[columnIndex] + " ");
        Console.WriteLine(row.ItemArray[columnIndex] + " ");
    }
}

这篇关于遍历数据表以从每一行获取两个特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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