foreach (DataGridViewRow v in dataGridView1.Rows) //判断第一列是否重复
{
if (v.Cells[0].Value != null)
{
var count = 0;
foreach (DataGridViewRow v2 in dataGridView1.Rows)
{
if (v2.Cells[0].Value != null)
{
if (v.Cells[0].Value.ToString().Equals(v2.Cells[0].Value.ToString()))
count++;
}
}
if (count > 1)
{
MessageBox.Show("第1列有重复,重复的内容是:【" + v.Cells[0].Value + "】");
return;
}
}
}
foreach (DataGridViewRow v in dataGridView1.Rows) //判断第一列是否重复
{
if (v.Cells[0].Value != null)
{
var count = 0;
DataGridViewRow dgvr = null;//////////
foreach (DataGridViewRow v2 in dataGridView1.Rows)
{
if (v2.Cells[0].Value != null && v != v2)
{
if (v.Cells[0].Value.ToString().Equals(v2.Cells[0].Value.ToString()))
{
count++;
dgvr = v2.Cells[0];
break;
}
}
}
if (count > 1)
{
MessageBox.Show("第1列有重复,重复的内容是:【" + v.Cells[0].Value + "】");
System.Timers.Timer t = new System.Timers.Timer(100);
t.Elapsed += (tsender, te) =>
{
t.Dispose();
this.Invoke(new setState(() =>
{
dataGridView1.CurrentCell = dgvr;///////////////////
dataGridView1.BeginEdit(true);
}));
};
t.Enabled = true;
return;
}
}
}
https://ask.csdn.net/questions/692072#answer_566032
参考这个,设置CurrentCell,重新BeginEdit
private delegate void setState();///什么一个委托
foreach (DataGridViewRow v in dataGridView1.Rows) //判断第一列是否重复
{
if (v.Cells[0].Value != null)
{
var count = 0;
foreach (DataGridViewRow v2 in dataGridView1.Rows)
{
if (v2.Cells[0].Value != null)
{
if (v.Cells[0].Value.ToString().Equals(v2.Cells[0].Value.ToString()))
count++;
}
}
if (count > 1)
{
MessageBox.Show("第1列有重复,重复的内容是:【" + v.Cells[0].Value + "】");
System.Timers.Timer t = new System.Timers.Timer(100);
t.Elapsed += (tsender, te) =>
{
t.Dispose();
this.Invoke(new setState(() =>
{
dataGridView1.CurrentCell = v.Cells[0];//////
dataGridView1.BeginEdit(true);
}));
};
t.Enabled = true;
return;
}
}
}