Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
datagridview dgv is populated from a datatable dt.
Within dgv I can add records as the grid allows adding a new record.
Before clicking on the save button, how can I check if the added records is already present in the dgv?

Thanks
Posted

Can you please post the code where you bind/populate the data to the grid please ?
 
Share this answer
 
To prevent duplicate records while the grid is being edited, handle RowValidating. In the handler, check that the record is unique and do not allow the user to exit the row unless it is. That way, your grid can only ever contain unique records.
Perform the same check in the Validating event (grid validation) in case you are creating a duplicate when you click save (although the RowValidating event will fire, in this case it won't stop the user exiting the row).

If you want to allow duplicate records until Save is clicked, just do verification in the Validating event handler. Then the user cannot leave the grid until the duplicate is deleted.

I've assumed that you have the knowledge to check for duplicates. You need to parse the DataTable to check that.
 
Share this answer
 
v2
Comments
arkiboys 26-Sep-11 11:05am    
Hi, do you mean in the rowvalidating event of the datagridview, I should compare the added row to the unchanged rows in the datatable?
C#
  var rowCollection = dataGridView1.Rows.OfType<datagridviewrow>().Where(r => !r.IsNewRow).ToList();

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.IsNewRow)
                {
                    rowCollection.ForEach(f =>
                        {
                            //code to check on duplication
                        });
                }
            }

</datagridviewrow>
 
Share this answer
 
Comments
arkiboys 26-Sep-11 11:08am    
I am not familiar with lambada expression. Note that I am using datatable dt to populate grid. I think I have to check for duplicate sin the RowValidating event but not sure how?
Thanks
this method check the already added product in the datagrid

if the product exist then method return false , else return true....
C#
public bool Check_Product_Exists()
      {
          bool result = true;
          string product = "";
          for (int b = 0; b < grd_dtls.Rows.Count - 1; b++)
          {
             
              for (int c = 0; c < grd_dtls.Rows.Count - 1; c++)
              {
                  if (c != b)
                  {
                      if (product.Equals(grd_dtls.Rows[c].Cells[0].Value))
                      {
                          grd_dtls.Rows[c].Selected = true;
                          MessageBox.Show("You have selected a product twice.");
                          return result = false;
                      }
                  }
              }
          }
          return result;
      }


use this code put your grid name instead of mine
 
Share this answer
 
v2

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