搜索在DataGridView中和过滤它 [英] searching in datagridview and filtering it
本文介绍了搜索在DataGridView中和过滤它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码就一个问题,我使用的BindingSource来显示数据和该代码只能选择该行,当即时通讯在datagridview的搜索。我想知道我怎么可以过滤数据即时搜索。
私人无效的button1_Click(对象发件人,EventArgs五)
{
串searchValue = textBox1.Text;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
试
{
的foreach(的DataGridViewRow行dataGridView1.Rows)
{
如果(row.Cells [2] .Value.ToString() .Equals(searchValue))
{
row.Selected = TRUE;
中断;
}
}
}
赶上(例外EXC)
{
MessageBox.Show(exc.Message);
}
}
解决方案
如果要只显示过滤的行使用 BindingSource.Filter
属性。
这里是 MSDN一个很好的样本
bindingSource.Filter =COLUMNNAME ='价值';
私人无效的button1_Click(对象发件人,EventArgs五)
{
串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屋!
查看全文