【发布时间】:2019-11-05 20:53:09
【问题描述】:
我有一个 datagridview 包含许多行的数字。我有一个数据表,其中一列包含数字。
我想突出显示不在数据表行中的 datagridview 行。我只比较一列。
这是我的代码:
DataTable SeatNum = new DataTable();
SeatNum = "Select Nums from dbo.Nums";
try
{
foreach (DataGridViewRow row in dataGridView2.Rows)
{
if (row.IsNewRow) { return; }
foreach (DataRow dtrow in SeatNum.Rows)
{
if (dtrow[0].ToString() != (row.Cells[0].Value.ToString()))
{
row.Cells[0].Style.BackColor = Color.Red;
MessageBox.Show("Not Exist" + row.Cells[0].Value.ToString() + "\r\n" + dtrow[0].ToString(), "Caution", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
break;
}
else
{
row.Cells[0].Style.BackColor = row.DefaultCellStyle.BackColor;
MessageBox.Show("Exist" + row.Cells[0].Value.ToString() + "\r\n" + dtrow[0].ToString(), "Caution", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
break;
}
}
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
此代码仅突出显示与数据表中第一行不匹配的行
提前谢谢你。
【问题讨论】:
-
第二个
foreach循环看起来很奇怪。foreach (DataRow dtrow in SeatNum.Rows)... 然后在if语句中,thenANDelse条件中有一个break。因此,循环只会执行一次。 -
当我删除中断时,所有行都被突出显示。
标签: c# datagridview datatable match highlight