C#如何更改我的代码
本文关键字:代码 我的 何更改 | 更新日期: 2023-09-27 18:27:38
编写所需单词显示在文本框中。我想要多行。。。但是,通过这个代码,我只选择了一行。。并且我想要输出到屏幕行。。我希望只显示指定的行(与"文本"框的文本匹配),而不是绘制。如何做到这一点?
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");
}
如果要根据搜索框筛选数据,则应将数据放入datatable
,筛选dataview
,并将dataview
绑定到datagridview
。(你可以在这里和这里学习)
但是,如果你想更改当前代码,那么你应该删除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");
}