DataTable利用内置函数过滤数据,RowFilter用法,写法如下:
string strName = string.Empty;//查询条件 string strSQL = string.Empty;//SQL DataView datav = dt.DefaultView; //利用Defaultview if (!string.IsNullOrEmpty(textBox1.Text.Trim())) { strName = textBox1.Text.Trim(); } strSQL = " userName like '%" + strName + "%'"; datav.RowFilter = strSQL; this.dgv_DataSource.DataSource = datav;
利用DataTable的 select内置函数,写法如下:
string strName = string.Empty;//查询条件 string strSQL = string.Empty;//SQL if (!string.IsNullOrEmpty(textBox1.Text.Trim())) { strName = textBox1.Text.Trim(); } strSQL = " 1=1 And"; strSQL += " userName like '%" + strName + "%'"; DataRow[] dr = dt.Select(strSQL);
if (dr.Length > 0)
{ DataTable temp = dr[0].Table.Clone();//把搜索出来的DataRow转换成Datatable if (dr != null && dr.Length > 0) { foreach (DataRow item in dr) { temp.Rows.Add(item.ItemArray); } }
} this.dgv_DataSource.DataSource = temp;