【问题标题】:using two textboxes for filter two columns of gridview使用两个文本框过滤两列gridview
【发布时间】:2018-03-14 06:30:09
【问题描述】:

我想根据来自两个文本框的输入过滤我的网格视图。 我有什么:

 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”。

【问题讨论】:

  • 那么,问题是什么?
  • 对不起,我不明白这个问题,请您详细说明
  • 我正在获取一个文件夹的目录并根据条件将其拆分...拆分文件名后数据分为三列。第一是emp_no,第二是数据。我只想条件两个文本框按 emp 和 date 过滤数据

标签: c# c#-4.0 c#-3.0


【解决方案1】:

除了设置RowFilter 的值外,您还需要(再次)将数据绑定到gridview,以便在UI 上更新。

因此,您需要分配数据源,并在网格上调用DataBind() - 在这两种方法中。像这样,

GridViewMain.DataSource = dataView;
GridViewMain.DataBind();

【讨论】:

    【解决方案2】:

    我自己做的,但感谢所有试图帮助我的人。 :)

         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 + "%'");
         }
    

    【讨论】:

      【解决方案3】:

      用这个sn-p来实现你所需要的

        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();
      

      来自微软官方 MSDN:

      使用 DataBind() 方法将数据从数据源绑定到 GridView 控件。此方法解析控件活动模板中的所有数据绑定表达式。

      【讨论】:

      • 我没有数据库连接。实际上我在网格列中显示文件夹的目录
      • GridView1.DataBind();
      • 为你编辑了我的答案
      • 我可以把我的完整代码发给你吗?因为数据绑定会报错
      • 也更新了我的答案
      猜你喜欢
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-07
      • 2013-06-24
      • 2014-11-17
      相关资源
      最近更新 更多