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

2023-09-03 04:32:09 作者:大概是你喜淡泊而我姓金

我想通过在数据表中某一列的值循环?任何人都可以请给C#代码?

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

推荐答案

喜试试下面code段

Hi try below code snippet


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

命名空间ConsoleApplication1 {     类节目     {         公共静态数据表GetDataTable()         {             //创建一个新的DataTable对象             数据表objDataTable =新的DataTable();             //创建一个字符串三列它们的类型             objDataTable.Columns.Add(列1中的typeof(字符串));             objDataTable.Columns.Add(列2中的typeof(字符串));             objDataTable.Columns.Add(列3中的typeof(字符串));             //添加一些数据在此数据表中的行             objDataTable.Rows.Add(新的String [] {ROW1 - 列1,ROW1 - 列2,ROW1 - Column3});             objDataTable.Rows.Add(新的String [] {的Row2 - 列1,的Row2 - 列2,的Row2 - Column3});             objDataTable.Rows.Add(新的String [] {ROW3 - 列1,ROW3 - 列2,ROW3 - Column3});             回报objDataTable;

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;

}