DataGridview search: show only search and hide other rows?

What should I add to my code to show the results of my search?

Right now, when I search for a search, it is selected (highlighted) and the rest remain the same.

tried to hide other lines, but without any success (and only show searchresult, one). Any suggestions? Im using datagridview.

My code is:

private void button3_Click_1(object sender, EventArgs e) { string search = textBox1.Text; for (int i = 0; i < dgTest.Rows.Count; i++) { if (dgTest.Rows[i].Cells[0].Value.ToString() == search) { dgTest.Rows[i].Selected = true; break; } else { dgTest.Rows[i].Selected = false; } } } 
+7
source share
2 answers

you can try the following:

 for (int i = 0; i < dgTest.Rows.Count; i++) { if (dgTest.Rows[i].Cells[0].Value.ToString() == "search") { dgTest.Rows[i].Selected = true; dgTest.Rows[i].Visible = true; } else { dgTest.Rows[i].Visible = false; dgTest.Rows[i].Selected = false; } } 
+1
source

If your DataGridView is not bound to a data source, then setting the Visible property to false will hide it:

 for (int i = 0; i < dgTest.Rows.Count; i++) { var row = dgTest.Rows[i]; if (row.Cells[0].Value.ToString() == search) { row.Selected = true; row.Visible = true; } else { row.Selected = false; row.Visible = false; } } 

(I deleted the "break" command, because even after you find a matching line, you need to continue and hide the other lines.)

If you use a DataBinding, it is not as simple as shown on this page .

+5
source

All Articles