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

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

问题描述

我有一个关于这个代码的问题,我使用绑定源显示数据,这个代码只在datagridview中进行搜索时选择该行。我想知道如何过滤数据搜索。

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

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

尝试
{
foreach(DataGridView1.Rows中的DataGridViewRow行)
{
if(row.Cells [2] .Value.ToString() .Equals(searchValue))
{

row.Selected = true;
break;

}
}
}
catch(异常exc)
{
MessageBox.Show(exc.Message);
}
}


解决方案

if您只想显示过滤的行,使用 BindingSource.Filter 属性。
以下是 msdn 中的一个很好的示例/ p>

  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);
//如果您需要

过滤器使用以下

  bindingSource.RemoveFilter(); 
//或
bindingSource.Filter = null;


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

解决方案

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;

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

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