Help with Find() in the BindingSource

10,817

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 ;
}
Share:
10,817
DanSogaard
Author by

DanSogaard

Updated on June 04, 2022

Comments

  • DanSogaard
    DanSogaard almost 2 years

    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?.

  • Hilal Al-Rajhi
    Hilal Al-Rajhi almost 8 years
    Then how can I display the first column in that position?
  • Jacob Seleznev
    Jacob Seleznev almost 8 years
    dataGridView1.CurrentCell = dataGridView1.CurrentRow.Cells[0];
  • Hilal Al-Rajhi
    Hilal Al-Rajhi almost 7 years
    If you don't set position to value >= 0 then its binding source default position is 0. Better way is: int pos=bindingsource.find("..", val) then use pos to check if search returned value if = -1 or >=0