循环遍历数据表

Looping through a DataTable(循环遍历数据表)

本文介绍了循环遍历数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯.我有一个包含多列和多行的 DataTable.

Well. I have a DataTable with multiple columns and multiple rows.

我想动态循环遍历 DataTable,基本上输出应该如下所示,不包括大括号:

I want to loop through the DataTable dynamically basically the output should look as follows excluding the braces :

Name (DataColumn)
Tom  (DataRow)
Peter (DataRow)

Surname (DataColumn)
Smith (DataRow)
Brown (DataRow)

foreach (DataColumn col in rightsTable.Columns)
{
     foreach (DataRow row in rightsTable.Rows)
     {
          //output              
     }
} 

我把它打了出来,发现这行不通.有人可以建议更好的方法吗?

I typed that out and noticed this would not work. Can someone please advice on a better way of doing this?

推荐答案

foreach (DataColumn col in rightsTable.Columns)
{
     foreach (DataRow row in rightsTable.Rows)
     {
          Console.WriteLine(row[col.ColumnName].ToString());           
     }
} 

这篇关于循环遍历数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:循环遍历数据表

基础教程推荐