locked
How to check duplicate data in datagridview RRS feed

  • Question

  • Hi everyone,
    Just want to ask how to check and block duplicate data in my datagridview

    I do have this code for inserting data in my datagridview
    DataGridViewRow addRow = new DataGridViewRow();
    newRow.CreateCells(dataGridView1);
    addRow.Cells[0].Value = labelOne.Text.ToString();
    dataGridView1.Rows.Add(newRow);
    
    This is when i click an "Add" button.

    How to check and block if there is any other exiting value in my grid?

    Regards,
    nemesis
    Friday, January 29, 2010 4:31 AM

Answers

  • Hi,
    hope it helps
    for (int row = 0; row < dataGridView1.Rows.Count; row++)
    
    {
    
    for (int col = 0; col < dataGridView1.Columns.Count; col++)
    
    {
    
    	if (dataGridView1.Rows[row].Cells[col].Value != null &&
    
    	  dataGridView1.Rows[row].Cells[col].Value.Equals(label1.Text.Trim()))
    
    	{
    
    		MessageBox.Show("Duplicate");
    
    	}
    
    	else
    
    	{
    
    		//Add To datagridview
    
    	}
    
    }
    
    }
    
    

    Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you

    Hi nemesiscsharp,Users MedalsUsers MedalsUsers Medals

    Just need to change Gnanadurai's sample code a little.

    private void Add_Click(object sender, EventArgs e)
    {
        for (int row = 0; row < dataGridView2.Rows.Count; row++)
        {
            for (int col = 0; col < dataGridView2.Columns.Count; col++)
            {
                if (dataGridView2.Rows[row].Cells[col].Value != null &&
                  dataGridView2.Rows[row].Cells[col].Value.Equals(label1.Text.Trim()))
                {
                    MessageBox.Show("Duplicate");
                    return;
                } 
            }
        }
    
        DataGridViewRow addRow = new DataGridViewRow();
        addRow.CreateCells(dataGridView2);
        addRow.Cells[0].Value = label1.Text.ToString();
        dataGridView2.Rows.Add(addRow);            
    }
    


    Best regards,
    Alex Liang
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by nemesiscsharp Monday, February 1, 2010 3:30 PM
    Monday, February 1, 2010 6:10 AM

All replies

  • Hi,
    hope it helps
    for (int row = 0; row < dataGridView1.Rows.Count; row++)
    {
    for (int col = 0; col < dataGridView1.Columns.Count; col++)
    {
    	if (dataGridView1.Rows[row].Cells[col].Value != null &&
    	  dataGridView1.Rows[row].Cells[col].Value.Equals(label1.Text.Trim()))
    	{
    		MessageBox.Show("Duplicate");
    	}
    	else
    	{
    		//Add To datagridview
    	}
    }
    }

    Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
    Friday, January 29, 2010 4:42 AM
  • hi,
    
    
            System.Collections.ArrayList arrListIDs = new System.Collections.ArrayList();
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    int empID=int.Parse(dataGridView1.Rows[0].Cells["EmpID"].Value.ToString());
                    if (!arrListIDs.Contains(empID))
                    {
                        arrListIDs.Add(empID);
                        //do ur code
                    }
                    else
                    {
                        MessageBox.Show("Duplicate row!");
                    }
                }
    
    
    check this, and let me know if it is related, else fell free to ask.........
    Nagarjuna Dilip
    Friday, January 29, 2010 2:25 PM
  • @Gnanadurai : i follow your code sir but the problem is i came into an infinite loop..

    @NagarjunaDilip : What do you mean with this code?


    dataGridView1.Rows[0].Cells["EmpID"
    ].Value.ToString()

    Is the "EmpID" the name of my column? but i came into a null reference or the column name doesn't exist.. i just follow the name of columns in my gridview
    Sunday, January 31, 2010 5:26 AM
  • Here is a solution from other forum -
    The first will report true/false if the DataTable has duplicate records, the second will remove any duplicates from the Row collection:
    
        bool HasDuplicates(System.Data.DataTable dataTable)
        {
          System.Data.DataTable duplicateTable = dataTable.Clone();
          System.Data.DataColumn[] primaryKey = new System.Data.DataColumn[duplicateTable.Columns.Count];
          duplicateTable.Columns.CopyTo(primaryKey, 0);
          duplicateTable.PrimaryKey = primaryKey;
    
          foreach(System.Data.DataRow dataRow in dataTable.Rows)
            if(duplicateTable.Rows.Contains(dataRow.ItemArray))
              return true;
            else
              duplicateTable.Rows.Add(dataRow.ItemArray);
          
          return false;
        }
    
        void RemoveDuplicates(System.Data.DataTable dataTable)
        {
          System.Data.DataTable duplicateTable = dataTable.Clone();
          System.Data.DataColumn[] primaryKey = new System.Data.DataColumn[duplicateTable.Columns.Count];
          duplicateTable.Columns.CopyTo(primaryKey, 0);
          duplicateTable.PrimaryKey = primaryKey;
          
          System.Data.DataRow[] dataRows = new System.Data.DataRow[dataTable.Rows.Count];
          dataTable.Rows.CopyTo(dataRows, 0);
          foreach(System.Data.DataRow dataRow in dataRows)
            if(duplicateTable.Rows.Contains(dataRow.ItemArray))
              dataTable.Rows.Remove(dataRow);
            else
              duplicateTable.Rows.Add(dataRow.ItemArray);
        }

    Sunday, January 31, 2010 6:03 AM
  • @ashraf882: how to use this function? what am i suppose to put in my HasDuplicates( ? ) method?
    Sunday, January 31, 2010 2:34 PM
  • hi,

    EmpID is nothing but ur DB column name....if u want to check duplicate row in a datagridview then this the code........
               System.Collections.ArrayList arrListIDs = new System.Collections.ArrayList();
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    int empID=int.Parse(dataGridView1.Rows[0].Cells["EmpID"].Value.ToString());
                    if (!arrListIDs.Contains(empID))
                    {
                        arrListIDs.Add(empID);
                        //do ur code
                    }
                    else
                    {
                        MessageBox.Show("Duplicate row!");
                    }
                }
    

    if you want to check before binding to datagrid then use in this way....

    DataTable dtValues =objDAO.GetValues();
    
    dataGridView1.DataSource =  dtValues.DefaultView.ToTable(true, "EmpID");
    

    Nagarjuna Dilip
    Monday, February 1, 2010 5:41 AM
  • Hi,
    hope it helps
    for (int row = 0; row < dataGridView1.Rows.Count; row++)
    
    {
    
    for (int col = 0; col < dataGridView1.Columns.Count; col++)
    
    {
    
    	if (dataGridView1.Rows[row].Cells[col].Value != null &&
    
    	  dataGridView1.Rows[row].Cells[col].Value.Equals(label1.Text.Trim()))
    
    	{
    
    		MessageBox.Show("Duplicate");
    
    	}
    
    	else
    
    	{
    
    		//Add To datagridview
    
    	}
    
    }
    
    }
    
    

    Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you

    Hi nemesiscsharp,Users MedalsUsers MedalsUsers Medals

    Just need to change Gnanadurai's sample code a little.

    private void Add_Click(object sender, EventArgs e)
    {
        for (int row = 0; row < dataGridView2.Rows.Count; row++)
        {
            for (int col = 0; col < dataGridView2.Columns.Count; col++)
            {
                if (dataGridView2.Rows[row].Cells[col].Value != null &&
                  dataGridView2.Rows[row].Cells[col].Value.Equals(label1.Text.Trim()))
                {
                    MessageBox.Show("Duplicate");
                    return;
                } 
            }
        }
    
        DataGridViewRow addRow = new DataGridViewRow();
        addRow.CreateCells(dataGridView2);
        addRow.Cells[0].Value = label1.Text.ToString();
        dataGridView2.Rows.Add(addRow);            
    }
    


    Best regards,
    Alex Liang
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by nemesiscsharp Monday, February 1, 2010 3:30 PM
    Monday, February 1, 2010 6:10 AM
  • Thank you Alex, it works! :)

    Thank you MSDN. 

    Regards,
    nemesis
    Monday, February 1, 2010 3:30 PM