DataTable dt=bll_m.getlist("");//数据源
//添加operateStr 列
dt.Columns.Add("operateStr");
1.for 循环赋值
for(int i=0;i<dt.rows.count;i++)
{
//为新添加的列进行赋值
dt.Row[i]["operateStr"]="";
}
2.foreach 循环赋值
foreach(DataRow dr in dt.rows)
{
//为新添加的列进行赋值
dr["operateStr"]="";
}
获取DataTable某一列的所有值
/// <summary>
/// 获取某一列的所有值
/// </summary>
/// <typeparam name="T">列数据类型</typeparam>
/// <param name="dtSource">数据表</param>
/// <param name="filedName">列名</param>
/// <returns></returns>
public static List<T> GetColumnValues<T>(DataTable dtSource,string filedName)
{
return (from r in dtSource.AsEnumerable() select r.Field<T>("ID")).ToList<T>();
}
方法调用:
获取字段ID的所有值
DataTable dt = ......;
List<int> ids= DataTablePro.GetColumnValues<int>(dt, "ID");
直接使用:
//遍历DataTable,取出所有的ID :
List<int> lstID = (from d in dt.AsEnumerable() select d.Field<int>("ID")).ToList();