C# DataGridView columns 1 与 columns 1 比较像这个图像
C# DataGridView columns 1 compare with columns 1 like this image
我要比较:
if(already employee id == last cell employee)
{
MessagBox.show(Your Value is duplicated)
}
这一点为您指明了正确的方向:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
// Data of inserted value
var rowIndex = e.RowIndex;
var colIndex = e.ColumnIndex;
var insertedValue = (dataGridView1[colIndex, rowIndex].Value ?? "").ToString();
foreach(DataGridViewRow row in dataGridView1.Rows)
{
// If current cell == insertedValue but not the same row
if((row.Cells["ColumnID"].Value ?? "").ToString() == insertedValue && row.Index != rowIndex)
{
MessageBox.Show("Your Value is duplicated");
}
}
}
Linq也是可以的:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
var insertedValue = (dataGridView1[e.ColumnIndex, e.RowIndex].Value ?? "").ToString();
dataGridView1.Rows.Cast<DataGridViewRow>()
.Where
(
x => (x.Cells["ColumnID"].Value ?? "").ToString() == insertedValue
&& x.Index != e.RowIndex
)
.ToList()
.ForEach(x => MessageBox.Show("Your Value is duplicated"));
}
注意这个不会删除插入的值。因此,如果您再次插入相同的值,MessageBox
将增加 2 倍。
我要比较:
if(already employee id == last cell employee)
{
MessagBox.show(Your Value is duplicated)
}
这一点为您指明了正确的方向:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
// Data of inserted value
var rowIndex = e.RowIndex;
var colIndex = e.ColumnIndex;
var insertedValue = (dataGridView1[colIndex, rowIndex].Value ?? "").ToString();
foreach(DataGridViewRow row in dataGridView1.Rows)
{
// If current cell == insertedValue but not the same row
if((row.Cells["ColumnID"].Value ?? "").ToString() == insertedValue && row.Index != rowIndex)
{
MessageBox.Show("Your Value is duplicated");
}
}
}
Linq也是可以的:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
var insertedValue = (dataGridView1[e.ColumnIndex, e.RowIndex].Value ?? "").ToString();
dataGridView1.Rows.Cast<DataGridViewRow>()
.Where
(
x => (x.Cells["ColumnID"].Value ?? "").ToString() == insertedValue
&& x.Index != e.RowIndex
)
.ToList()
.ForEach(x => MessageBox.Show("Your Value is duplicated"));
}
注意这个不会删除插入的值。因此,如果您再次插入相同的值,MessageBox
将增加 2 倍。