在文本框中显示所需的单词。我想要多行...但是,通过这段代码,我只选择了一行.. 我想输出到屏幕行..而不是画,我想只显示指定的行(匹配文本框的文本)。怎么做到这一点?
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");
}
答案 0 :(得分:0)
如果您想根据搜索框过滤数据,则应将数据放入datatable
并过滤dataview
并将dataview
绑定到datagridview
。 (您可以在here和here)
但是,如果您想更改当前代码,则应删除return
,因为它将从该行返回并且不会与其他行匹配
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");
}