Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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;            
                    }
               
            }
            



        }


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
Posted
Updated 25-Aug-18 2:07am

You may have to use CellEndEdit Event


C#
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)
           {

           }
       }
 
Share this answer
 
Another Solution Is
VB
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
 
Share this answer
 
v3
Comments
CHill60 7-Oct-15 18:46pm    
Question is 4 years old and tagged as C#. You have provided a VB solution that is essentially just a (poor) translation of the solution posted in 2011
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;
               }

           }
 
Share this answer
 
v3
Comments
Nelek 25-Aug-18 16:46pm    
do you realize that the question is from 2011?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900