1、把数据集合datatable中的字段转换程数组
public static string[] ConvertList(DataTable dt, string colName)
{
List<string> list = new List<string>();
DataTable dtzh = dt.DefaultView.ToTable(true, colName);
dtzh .DefaultView.Sort = colName;
dtzh = dtzh .DefaultView.ToTable();
foreach (DataRow dr in dtzh .Rows)
{
if (dr[0] != DBNull.Value && dr[0] != null)
{
list.Add(dr[0].ToString());
}
}
return list.ToArray();
}
其中红色的datatable是数据集合,绿色的colname 是数组中的字段名称,list 为返回的转换后的colname 列的数据集合。