gpt4 book ai didi

c# - 在 datagridview 中搜索并过滤它

转载 作者:行者123 更新时间:2023-11-30 13:58:07 27 4
gpt4 key购买 nike

我有一个关于此代码的问题,我使用绑定(bind)源来显示数据,并且此代码仅在我在 datagridview 中搜索时选择行。我想知道如何过滤正在搜索的数据。

 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 中的一个很好的示例

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
}

要删除过滤器,请使用以下命令

bindingSource.RemoveFilter();

bindingSource.Filter = null;

关于c# - 在 datagridview 中搜索并过滤它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18253624/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com