如何循环遍历数据表中特定列的值?

发布于 2024-10-05 11:08:47 字数 35 浏览 0 评论 0原文

我想循环访问数据表中特定列的值?谁能给出 C# 编码吗?

I want to loop through the values of a particular column in datatable? can anyone please give the C# coding?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我是有多爱你 2024-10-12 11:08:47
DataTable tbl = new DataTable();
foreach (DataRow row in tbl.Rows)
{
    object cellData = row["colName"];
}
DataTable tbl = new DataTable();
foreach (DataRow row in tbl.Rows)
{
    object cellData = row["colName"];
}
清晨说晚安 2024-10-12 11:08:47

您好,尝试下面的代码片段

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

命名空间 ConsoleApplication1 { 班级计划 { 公共静态数据表 GetDataTable() { //创建一个新的DataTable对象 数据表 objDataTable = new DataTable(); //创建三列,类型为字符串 objDataTable.Columns.Add("第 1 列", typeof(string)); objDataTable.Columns.Add("第 2 列", typeof(string)); objDataTable.Columns.Add("第 3 列", typeof(string)); //在此DataTable的行中添加一些数据 objDataTable.Rows.Add(new string[] { "行1 - 列1", "行1 - 列2", "行1 - 列3" }); objDataTable.Rows.Add(new string[] { "行2 - 列1", "行2 - 列2", "行2 - 列3" }); objDataTable.Rows.Add(new string[] { "Row3 - Column1", "Row3 - Column2", "Row3 - Column3" }); 返回objDataTable;

} static void Main(string[] args) { foreach (DataRow row in GetDataTable().Rows) { object cellData = row["Column 1"]; Console.WriteLine(cellData); } } }

}

Hi try below code snippet

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1 { class Program { public static DataTable GetDataTable() { //Create a new DataTable object DataTable objDataTable = new DataTable(); //Create three columns with string as their type objDataTable.Columns.Add("Column 1", typeof(string)); objDataTable.Columns.Add("Column 2", typeof(string)); objDataTable.Columns.Add("Column 3", typeof(string)); //Adding some data in the rows of this DataTable objDataTable.Rows.Add(new string[] { "Row1 - Column1", "Row1 - Column2", "Row1 - Column3" }); objDataTable.Rows.Add(new string[] { "Row2 - Column1", "Row2 - Column2", "Row2 - Column3" }); objDataTable.Rows.Add(new string[] { "Row3 - Column1", "Row3 - Column2", "Row3 - Column3" }); return objDataTable;

} static void Main(string[] args) { foreach (DataRow row in GetDataTable().Rows) { object cellData = row["Column 1"]; Console.WriteLine(cellData); } } }

}

帅冕 2024-10-12 11:08:47

尝试下面基于 Linq 的解决方案

var values = (from t in dt.AsEnumarable() // dt is your data table
             select t[ColumnName]).ToList().ForEach(your expression );


 or try normal way 

 foreach(DataRow drow in dt.Rows)
 {
      string value = drow[columnname].ToString();
 }

try below Linq based solution

var values = (from t in dt.AsEnumarable() // dt is your data table
             select t[ColumnName]).ToList().ForEach(your expression );


 or try normal way 

 foreach(DataRow drow in dt.Rows)
 {
      string value = drow[columnname].ToString();
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文