如何检查datagridview单元格中的重复值 [英] how to check repeated values in a datagridview cell
问题描述
我正在使用datagridview。我想限制用户在列UNITSLNO中输入重复值。当用户重复输入此列中的值(多次)时,将自动删除该值(重复输入)。我使用以下代码实现此目的
I am having a datagridview . I want to restrict the user from entering repeated value in a column ,"UNITSLNO". when a user enter a value in this column repeatedly(more than one time ) the value (repeatedly entered) will be deleted automatically.I achieved this with the following code
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
int count = dataGridView1.Rows.Count;
for (int i = 0; i <= count - 3; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value.ToString() == dataGridView1.Rows[count - 2].Cells[0].Value.ToString())
{
dataGridView1.Rows.RemoveAt(count - 2);
count = count - 1;
}
}
}
这是有效的,除了某些问题,例如当列包含单个数字(列类型是整数并且绑定到MS Access 2007)时,它不允许输入以相同的数字开头的任何数字已存在的单位数字。也就是说,当我尝试输入31或32等时,如果此列中已存在3,则不允许我完全输入该数字。实际发生的是,只要我在单元格中输入3,就会调用dataGridView1_CellValueChanged事件并执行删除重复行的代码。我尝试使用cell_leave事件也发生了同样的结果。我应采取什么方法来摆脱这一点。
提前感谢
this is working except certain problems such as when the column contains a single digit number (the column type is integer and bounded to MS Access 2007)it do not allows to enter any number which start with the same single digit number which already exist. ie if 3 already exist in this column when I try to enter 31 or 32 etc it do not allows me to completely enter the number . what actually happens is as soon as i enter 3 in the cell, dataGridView1_CellValueChanged event is invoked and the code to delete the repeated row is executed. I tried with cell_leave event too same result happened. what approach I should take to get rid of this .
thanks in advance
推荐答案
您可能必须使用CellEndEdit活动
You may have to use CellEndEdit Event
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex == 0) //VALIDATE FIRST COLUMN
for (int row = 0; row < dataGridView1.Rows.Count-1; row++)
{
if (dataGridView1.Rows[row].Cells[0].Value != null &&
row != e.RowIndex &&
dataGridView1.Rows[row].Cells[0].Value.Equals(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value))
{
MessageBox.Show("Duplicate");
} else
{
//Add To datagridview
}
}
}
catch (Exception ex)
{
}
}
另一种解决方案
Another Solution Is
Private Sub DGV_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DGV.CellEndEdit
Dim iCol = lvwBill.CurrentCell.ColumnIndex
Dim iRow = lvwBill.CurrentCell.RowIndex
Dim cc As Integer = 0
If iCol = 0 Then
For x = 0 To lvwBill.Rows.Count - 1
If DGV.Rows(x).Cells(iCol).Value = DGV.CurrentRow.Cells(iCol).Value Then
cc += 1
End If
Next
If cc > 1 Then
MessageBox.Show("Value Already Added in this Gridview")
DGV.CurrentCell = DGV(iCol, iRow)
Exit Sub
End If
' You can make other checking or validations
End If
End Sub
int count = dataGridView1.Rows.Count;
for (int i = 0; i <= count - 1; i++)
{
if (i == count - 2) return;
if (dataGridView1.Rows[i].Cells[2].Value.ToString() == dataGridView1.Rows[count - 2].Cells[2].Value.ToString())
{
//dataGridView1.Rows.RemoveAt(count - 2);
MessageBox.Show("هذا الكود موجود من قبل في هذا الاذن !!!","إذن إضافة", MessageBoxButtons.OK,MessageBoxIcon.Information);
count = count - 1;
break;
}
}
这篇关于如何检查datagridview单元格中的重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!