How to filter DataGridView in C# Win Forms?

后端 未结 3 572
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-16 07:48

Guys I have created a simple datagridview through toolbox and selected data through wizard (no code in .cs file) from database. It is working flawlessly as you can see in th

相关标签:
3条回答
  • 2020-12-16 07:53

    Thank you everyone that has answered to my query, I really appreciate your help guys. You guys are the most helpful bunch.

    I have solved my problem by doing following modifications to my code :

        public void btnSearch_Click(object sender, EventArgs e)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = dataGridView1.DataSource;
            bs.Filter = dataGridView1.Columns[5].HeaderText.ToString() + " LIKE '%" + txtbxSearch.Text + "%'";
            dataGridView1.DataSource = bs;
        }
    

    Thank you again.

    0 讨论(0)
  • 2020-12-16 07:59

    I hope I got your problem well

    string whereClause = "ContactPerson=" +textbox.text;
    (datagridview.DataSource as DataTable).DefaultView.RowFilter = whereClause;
    
    0 讨论(0)
  • 2020-12-16 08:01

    Try this:

    foreach (System.Windows.Forms.DataGridViewRow r in MyGridView.Rows)
    {
          if ((r.Cells[5].Value).ToString().ToUpper().Contains(searchText.ToUpper()))
          {
                MyGridView.Rows[r.Index].Visible = true;
                MyGridView.Rows[r.Index].Selected = true;
          }
          else
          {
                MyGridView.CurrentCell = null;
                MyGridView.Rows[r.Index].Visible = false;
          }
     }
    
    0 讨论(0)
提交回复
热议问题