在datagridview中搜索并过滤它 [英] searching in datagridview and filtering it

查看:24
本文介绍了在datagridview中搜索并过滤它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此代码有疑问,我使用 bindingsource 来显示数据,而此代码仅在我在 datagridview 中搜索时选择该行.我想知道如何过滤正在搜索的数据.

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 can i filter the data im searching.

 private void button1_Click(object sender, EventArgs e)
    {
        string searchValue = textBox1.Text;

         dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

         try
         {
             foreach (DataGridViewRow row in dataGridView1.Rows)
             {
                 if (row.Cells[2].Value.ToString().Equals(searchValue))
                 {

                     row.Selected = true;
                     break;

                 }
             }
         }
         catch (Exception exc)
         {
             MessageBox.Show(exc.Message);
         }
    }

推荐答案

如果您只想显示过滤的行,请使用 BindingSource.Filter 属性.这是 MSDN

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

bindingSource.Filter = null;

这篇关于在datagridview中搜索并过滤它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆