您好,我正在尝试在从 XML 文件填充的两个 dataGridView 表中查找唯一字符串。我制作的代码运行没有问题,但是当我在其中一个表中更改字符串(使其唯一)时,它无法检测到。我的逻辑有什么问题吗?
private void button5_Click(object sender, EventArgs e)
{
string[] column1 = new string[dataGridView1.Rows.Count];
string[] column2 = new string[dataGridView2.Rows.Count];
int unique = 0;
bool found = false;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
column1[i] = Convert.ToString(dataGridView1.Rows[i].Cells[2].Value);
}
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
column2[i] = Convert.ToString(dataGridView2.Rows[i].Cells[2].Value);
}
for (int i = 0; i < column1.Length; i++)
{
for (int j = 0; j < column2.Length; j++)
{
if (column1[i] == column2[j])
{
found = true;
}
}
if (found == false)
{
unique++;
found = false;
}
}
MessageBox.Show(unique + " unique strings found!");
}
最终解决方案需要能够返回包含唯一字符串的单元格,以便我可以向用户突出显示它们。非常感谢你的帮助!