locked
Select a DataGridView row after TextBox value C# RRS feed

  • Question

  • I have a datagridview with 1 column with some rows. I want to do :
    When an user write a value in TextBox, if that value already exists in datagridview, I want to select the row which contains that TextInput value

    How to do that ?
    I'm going to use like this :

    dataGridView1.CurrentCell = dataGridView1[0, index];

    But I don't know how to find the index using TextBox value.

    Thank you

    Sunday, September 4, 2011 8:57 AM

Answers

  • Loop through the rows of DGV, and check which cell equals to the textBox value, then select the row if it does (or not select it if it doesnt) match.

    Example:

            public Form1()
            {
                InitializeComponent();
                dataGridView1.Columns.Add("col1", "column1");
                dataGridView1.Rows.Add("item 1");
                dataGridView1.Rows.Add("item 2");
                dataGridView1.Rows.Add("item 3");
                dataGridView1.Rows.Add("item 4");
                dataGridView1.Rows.Add("item 3");
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if (!dataGridView1.Rows[i].IsNewRow)
                    {
                        if (dataGridView1[0, i].Value.ToString() == textBox1.Text)
                            dataGridView1.Rows[i].Selected = true;
                        else
                            dataGridView1.Rows[i].Selected = false;
                    }
                }
            }
    


    Mitja
    • Proposed as answer by Neddy Ren Tuesday, September 6, 2011 6:48 AM
    • Marked as answer by Neddy Ren Wednesday, September 14, 2011 2:56 AM
    Sunday, September 4, 2011 9:12 AM
  • Is your DataGridView databound? If so, depending on how you've set its .DataSource, you may be able to easily find the row you're looking for. For example, if you've used a BindingSource and you're bound to a DataTable, your code might look something like this:

    // if you've databound your grid like this, to a DataTable:
    BindingSource bs = new BindingSource();
    bs.DataSource = MyDataTable;
    MyGrid.DataSource = bs;
    
    // then you can find the row you've looking for,
    // and position your grid to that row, like this:
    bs.Position = bs.Find("MyColumn", MyTextBox.Text);
    

    If your grid isn't databound at all (no .DataSource has been set), then you'll have to utilize Mitja's suggestion. If your grid.DataSource is set differently than what I posted above, you may have to utilize a different find mechanism. Post your databinding, if it's different ...

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Proposed as answer by Neddy Ren Tuesday, September 6, 2011 6:48 AM
    • Marked as answer by Neddy Ren Wednesday, September 14, 2011 2:56 AM
    Sunday, September 4, 2011 4:59 PM

All replies

  • Loop through the rows of DGV, and check which cell equals to the textBox value, then select the row if it does (or not select it if it doesnt) match.

    Example:

            public Form1()
            {
                InitializeComponent();
                dataGridView1.Columns.Add("col1", "column1");
                dataGridView1.Rows.Add("item 1");
                dataGridView1.Rows.Add("item 2");
                dataGridView1.Rows.Add("item 3");
                dataGridView1.Rows.Add("item 4");
                dataGridView1.Rows.Add("item 3");
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if (!dataGridView1.Rows[i].IsNewRow)
                    {
                        if (dataGridView1[0, i].Value.ToString() == textBox1.Text)
                            dataGridView1.Rows[i].Selected = true;
                        else
                            dataGridView1.Rows[i].Selected = false;
                    }
                }
            }
    


    Mitja
    • Proposed as answer by Neddy Ren Tuesday, September 6, 2011 6:48 AM
    • Marked as answer by Neddy Ren Wednesday, September 14, 2011 2:56 AM
    Sunday, September 4, 2011 9:12 AM
  • Is your DataGridView databound? If so, depending on how you've set its .DataSource, you may be able to easily find the row you're looking for. For example, if you've used a BindingSource and you're bound to a DataTable, your code might look something like this:

    // if you've databound your grid like this, to a DataTable:
    BindingSource bs = new BindingSource();
    bs.DataSource = MyDataTable;
    MyGrid.DataSource = bs;
    
    // then you can find the row you've looking for,
    // and position your grid to that row, like this:
    bs.Position = bs.Find("MyColumn", MyTextBox.Text);
    

    If your grid isn't databound at all (no .DataSource has been set), then you'll have to utilize Mitja's suggestion. If your grid.DataSource is set differently than what I posted above, you may have to utilize a different find mechanism. Post your databinding, if it's different ...

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Proposed as answer by Neddy Ren Tuesday, September 6, 2011 6:48 AM
    • Marked as answer by Neddy Ren Wednesday, September 14, 2011 2:56 AM
    Sunday, September 4, 2011 4:59 PM