获取DataTable选择第一行某一列值
数据源是一个DataTable,现在我们需要获取这个DataTable的第一行第一列的值。
先准备一个数据集,创建一个DataTable,并填充数据:
source code:
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using Insus.NET.Models; namespace Insus.NET.Entities { public class CustomerEntity { public DataTable Customers() { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("CustomerID", typeof(int))); dt.Columns.Add(new DataColumn("CustomerName", typeof(string))); dt.Columns.Add(new DataColumn("PID", typeof(string))); DataRow dr = dt.NewRow(); dr["CustomerID"] = 9; dr["CustomerName"] = "张三"; dr["PID"] = "123456789012"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["CustomerID"] = 10; dr["CustomerName"] = "李四"; dr["PID"] = "321245677812"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["CustomerID"] = 30; dr["CustomerName"] = "吴广"; dr["PID"] = "213445678912"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["CustomerID"] = 35; dr["CustomerName"] = "王维"; dr["PID"] = "334456789012"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["CustomerID"] = 36; dr["CustomerName"] = "赵勇"; dr["PID"] = "213445678912"; dt.Rows.Add(dr); return dt; } } }
......
方法很多,你可以择优使用。