We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Detect duplicate records

Hello!

How to properly detect duplicate input in grid?

I'm doing that on this way:

private void gridSirovina_CurrentRecordContextChange(object sender, CurrentRecordContextChangeEventArgs e)
if (e.Action == CurrentRecordAction.EndEditCalled)
            {
                Record rec = e.Record as Record;
                string myCode = Convert.ToString(rec.GetValue("SifraRobe"));

foreach (Record r in e.Table.Records)
                        {                         
                            string existValue = r.GetValue(gridSirovina.TableDescriptor.Fields["SifraRobe"]).ToString();
                            if (myCode == existValue)
                            {
                                MessageBox.Show("Exists!");
                                e.Cancel = true;
                                break;
                            }
                        }
}
}

But this works fine only in case when inserting new records. When changing existing record I get error that value exist even it is different than others in that specific cell. Is there any other way to efficient way to check duplicates?

1 Reply

MG Mohanraj Gunasekaran Syncfusion Team May 8, 2017 01:15 PM UTC

Hi Josip,  
 
Thanks for using Syncfusion products.  
 
 We have analyzed your provided code part. In your code part, you have tried to check the duplicate value by iterating through records collection . If you edit the cell value, the record value has updated in RecordsCollection so the MessageBox(“Exists”) appears even if it has a different value. In order to overcome this scenario, you can use the RowIndex property in CurrentRecordContextChange event. Please refer to the below code example,  
  
Code example,  
   
this.gridGroupingControl1.CurrentRecordContextChange += gridGroupingControl1_CurrentRecordContextChange;  
  
void gridGroupingControl1_CurrentRecordContextChange(object sender, Syncfusion.Grouping.CurrentRecordContextChangeEventArgs e)  
{  
    if (e.Action == Syncfusion.Grouping.CurrentRecordAction.EndEditCalled)  
    {  
        Record rec = e.Record as Record;  
        int newIndex = rec.GetRowIndex();  
        string myCode = Convert.ToString(rec.GetValue("Country"));  
        if (this.gridGroupingControl1.Table.CurrentRecord != null )  
        {  
            foreach (Record r in e.Table.Records)  
            {  
                int rowindex = r.GetRowIndex();  
                string existValue = r.GetValue(this.gridGroupingControl1.TableDescriptor.Fields["Country"]).ToString();  
                if (myCode == existValue && rowindex != newIndex)  
                {  
                    MessageBox.Show("Exists!");  
                    e.Cancel = true;  
                    break;  
                }  
            }  
        }  
    }  
}  
 
Sample link: GridGroupingCotnrol  
 
Regards,  
Mohanraj G  
 


Loader.
Up arrow icon