searching in datagridview and filtering it

前端 未结 2 1882
庸人自扰
庸人自扰 2020-12-12 00:33

i have a question regarding with this code, i use a bindingsource to show the data and this code only select the row when im searching in datagridview. i want to know how c

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-12 01:09

    if you want to display only the filtered rows use BindingSource.Filter property. Here is a good sample in MSDN

    bindingSource.Filter = "columnname = 'value'";
    
    private void button1_Click(object sender, EventArgs e)
    {
        string searchValue = textBox1.Text;
    
         dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
         bindingSource.Filter = string.Format("{0} = '{1}'","YourColumnName", searchValue );
         //here you can do selection if you need
    }
    

    To remove filter use the following

    bindingSource.RemoveFilter();
    

    or

    bindingSource.Filter = null;
    

提交回复
热议问题