c# - 有关 BindingSource 中 Find() 的帮助

标签 c# datagridview bindingsource

我用它来查找 DataGridView 中的值:

private void fndBtn_Click(object sender, EventArgs e)
        {
            BindingSource src = new BindingSource();
            src.DataSource = dataGridView1.DataSource;

            src.Position = src.Find("p_Name", textBox1.Text);
        }

但我有两个问题。首先,当我查找 dgv 中不存在的项目时,position 返回 0,默认情况下是第一行。我不希望这样,如果我使用 If 语句进行验证,我将丢失位置 0,从而丢失第一行。

第二是我希望关注行标题并突出显示找到的项目。这怎么可能?

最佳答案

像这样使用dataGridView的绑定(bind)源:

private void fndBtn_Click(object sender, EventArgs e)
{
    BindingSource src = new BindingSource();
    src.DataSource = dataGridView1.DataSource;
    int findedRow = 0;
    if (textBox1.Text!="")
          findedRow = src.Find("p_Name", textBox1.Text); 
    if (findedRow!=-1)
           src.Position = findedRow ;
}

关于c# - 有关 BindingSource 中 Find() 的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2421090/

相关文章:

c# - 如何检查gridview中的复选框是否被选中

c# - 在没有 For 表达式的情况下扩展 ASP.NET Core InputTagHelper 类时出现 NullReferenceException

c# - 我可以在未绑定(bind)的 DataGridView 中设置最大行数吗

c# - 过滤 DataGridView Winforms

c# - 如何对绑定(bind)到自定义对象集合的 DataGridView 进行排序?

c# - DataGridViewSelectedCellCollection 上的 LINQ 查询

c# - C# 结构与元组中的值语义

c# - 单击按钮(而不是单击单元格)更改 dataGridView 单元格格式

c# - 解除绑定(bind)绑定(bind)源

c# - 获取 bindingSource 的 CurrentRow 的实例