110,535
社区成员
发帖
与我相关
我的任务
分享
private void toolStripButton1_Click(object sender, EventArgs e)
{
if (this.toolStripTextBox1.Text.Equals(""))
{
return;
}
// Linq模糊查询
IEnumerable<DataGridViewRow> enumerableList = this.dataGridView1.Rows.Cast<DataGridViewRow>();
List<DataGridViewRow> list = (from item in enumerableList
where item.Cells[0].Value.ToString().IndexOf(this.toolStripTextBox1.Text) >= 0
select item).ToList();
// 恢复之前行的背景颜色为默认的白色背景
this.dataGridView1.Rows[beforeMatchedRowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.White;
if (list.Count > 0)
{
// 查找匹配行高亮显示
int matchedRowIndex = list[0].Index;
this.dataGridView1.Rows[matchedRowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Yellow;
this.beforeMatchedRowIndex = matchedRowIndex;
}
}
List<DataGridViewRow> list = (from item in enumerableList
where !item.IsNewRow && item.Cells[0].Value.ToString().IndexOf(this.toolStripTextBox1.Text) >= 0
select item).ToList();
或者List<DataGridViewRow> list = enumerableList.Where(dr => !dr.IsNewRow && dr.Cells[0].Value.ToString().IndexOf(this.toolStripTextBox1.Text) >= 0);