码迷,mamicode.com
首页 > Windows程序 > 详细

获取DataGridview中某列的所有数据

时间:2020-07-23 18:53:06      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:nec   style   fill   table   取整   pre   sel   order by   weight   

/// <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>(filedName)).ToList<T>();
}

调用

            string sql = "select sValueName,sTypeCode, sValueCode  , sRemark from dbo.pbDataDictionary where sTypeCode = ‘TubeWeight‘  and bUsable = 1 order by iOrderNo";

string connStr = "Data Source=.;Initial Catalog = test;Persist Security Info = True;User ID = zjp;Password =123 ;Connect Timeout=8;"

            SqlConnection Sqlconn = new SqlConnection(MainForm.connStr);
            SqlCommand cmd = new SqlCommand(sql, Sqlconn);
Sqlconn.Open();
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(sql, Sqlconn);

                DataTable dataTable = new DataTable();
                da.Fill(dataTable);//

  List<string> idCode = new List<string>();// 
  idCode = GetColumnValues<string>(dataTable, "sValueCode");//获取整列sValueCode的数据放到List中

 

获取DataGridview中某列的所有数据

标签:nec   style   fill   table   取整   pre   sel   order by   weight   

原文地址:https://www.cnblogs.com/tiancaige/p/13366378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!