![](/img/trans.png)
[英]Dynamically add Filter Expression to the Gridview to Filter Two Columns
[英]using two textboxes for filter two columns of gridview
我想根据来自两个文本框的输入来过滤网格视图。 我有的:
private void textBox1_TextChanged_1(object sender, EventArgs e)
{
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("[1] LIKE '%{0}%'", textBox1.Text);
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("[2] LIKE '%{0}%'", textBox2.Text);
}
例如,第一列是“ 123”,第二列是“ clark”。
使用此代码片段来实现您所需要的
DataSet ds = new DataSet();
SqlConnection myCon = new SqlConnection(connectionstring);
SqlDataAdapter adapter = new SqlDataAdapter(cmd, myCon);
adapter.Fill(ds);
DataView view = new DataView();
view.Table = ds.Tables[0];
view.RowFilter = "ColumnName = " + TextBox1.Text.Trim();
GridView1.DataSource = view;
GridView1.DataBind();
这是使用ADO.Net完成的。
编辑以匹配您的问题:
private void Form1_Load(object sender, EventArgs e)
{
DataTable resultTable = new DataTable();
string path = @"\\192.168.96.80\hrmspics";
recFolders(path, ref resultTable);
dataGridView1.DataSource = resultTable;
dataGridView1.DataBind();
从官方的Microsoft MSDN:
使用DataBind()方法将数据源中的数据绑定到GridView控件。 此方法解析控件的活动模板中的所有数据绑定表达式。
除了设置RowFilter
的值之外,您还需要将数据绑定到网格视图(再次),以便在UI上对其进行更新。
因此,您需要分配数据源,并在两种方法中的网格上调用DataBind()
。 像这样,
GridViewMain.DataSource = dataView;
GridViewMain.DataBind();
我做到了自己,但感谢所有尝试帮助我的人。 :)
private void textBox1_TextChanged_1(object sender, EventArgs e)
{
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("[1] LIKE '%" + textBox1.Text + "%' and [2] like '%" + textBox2.Text + "%'");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.