开发者

Help with Find() in the BindingSource

开发者 https://www.devze.com 2022-12-23 00:48 出处:网络
I use this to loo开发者_开发问答k for values in my DataGridView: private void fndBtn_Click(object sender, EventArgs e)

I use this to loo开发者_开发问答k 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?.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号