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");
}
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");
}