1

Write the desired word is displayed in the text box. I want to have multiple rows...but, by this code I get only one row selected.. and I want to output to the screen rows.. Rather than painted, I want to display only the rows that specified (match to the Text box's text). How to achieve this?

     try
        {

            dataGridView1.ClearSelection();   //or restore rows backcolor to default
            for (int i = 0; i < (dataGridView1.Rows.Count); i++)
            {
                for (int j = 0; j < (dataGridView1.Columns.Count); j++ )
                    if (dataGridView1.Rows[i].Cells[j].Value.ToString().StartsWith(txbSearchName.Text, true, CultureInfo.InvariantCulture))
                        //(dataGridView1.Rows[i].Cells[j].Value.ToString().StartsWith(txbSearchName.Text, true, CultureInfo.InvariantCulture))
                    {
                        dataGridView1.FirstDisplayedScrollingRowIndex = i;
                        dataGridView1.Rows[i].Selected = true; //It is also possible to color the row backgroud
                        return;
                    }
            }
        }
        catch (Exception)
        {
            MessageBox.Show("not exist");
        }
Sachin
  • 2,152
  • 1
  • 21
  • 43
Simkyujin
  • 115
  • 2
  • 2
  • 8

1 Answers1

0

If you want to filter data based on the search box then you should put your data into the datatable and filter the dataview and bind that dataview to datagridview. (You can learn that at here and here)

But if you want to change in your current code then you should remove return as it will return from that row and will not match other rows

        try
        {

            dataGridView1.ClearSelection();   //or restore rows backcolor to default
            for (int i = 0; i < (dataGridView1.Rows.Count); i++)
            {
                for (int j = 0; j < (dataGridView1.Columns.Count); j++ )
                    if ((dataGridView1.Rows[i].Cells[j].Value.ToString().StartsWith(txbSearchName.Text, true, CultureInfo.InvariantCulture)) == false)
                        //(dataGridView1.Rows[i].Cells[j].Value.ToString().StartsWith(txbSearchName.Text, true, CultureInfo.InvariantCulture))
                    {
                        //dataGridView1.FirstDisplayedScrollingRowIndex = i;
                        //dataGridView1.Rows[i].Selected = true; //It is also possible to color the row backgroud
                        //return;
                        dataGridView1.Rows.Remove(dataGridView1.Rows[i]);
                    }
            }
        }
        catch (Exception)
        {
            MessageBox.Show("not exist");
        }
Community
  • 1
  • 1
Sachin
  • 2,152
  • 1
  • 21
  • 43
  • oh, ok i get it. emm...one more question..i don't want to datatable.. because i can't using that..i'm beginner..I want to see in the text box to search only the cells through a screen..is there a way? – Simkyujin Dec 22 '15 at 06:33
  • 1
    this code will work for that also as it is searching in the datagrid's row only .. but keep in mind that if you have searched "hello" then it will show the rows starting with "hello" and remove other all & you can not get back them until you rebind datagrid from csv. And yes datatable is also easy for beginner.. you should try it.. – Sachin Dec 22 '15 at 06:36