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 ;
}
精彩评论