有关 BindingSource 中 Find() 的帮助

发布于 2024-08-24 20:19:37 字数 441 浏览 11 评论 0原文

我用它来查找 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,从而丢失第一行。

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

I use this to look for values in my 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);
        }

But I've two issues. First when I look for item that doesn't exist in my dgv, position returns 0 which is by default the first row. I don't want that, and if I verified using If statement I'll lose position 0, thus losing the first row.

Second is I want the row header be focused on and item found be highlighted. How is that possible?.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霓裳挽歌倾城醉 2024-08-31 20:19:37

像这样使用 dataGridView 的绑定源:

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

Use dataGridView's binding source like this:

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 ;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文