This is a migrated thread and some comments may be shown as answers.

Check for duplicates in RadGridView rows

4 Answers 645 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Rieni De Rijke
Top achievements
Rank 1
Rieni De Rijke asked on 12 May 2010, 12:03 PM

We have a RadGridView with 2 columns.
When we edit a cell or when we are adding a new row, we want to loop through the rows cell[1] to check for duplicates.
We want to do this in...

private void TheGridCellValidating(object sender, GridViewCellValidatingEventArgs e).

We try things like this...

            var grid = (e.Row).GridViewDataControl;
            string value = e.NewValue.ToString();
           
            var rows = grid.ChildrenOfType<GridViewRow>();

            foreach (var row in rows)
            {
                if (row == null) continue;
                if (row.Cells == null) continue;
                if (row.Cells[1] != null)
                {
                    var s = row.Cells[0]......
                    ...
                    ....
                }
            }
But... it won't work.
How can we iterate on the rows and check for duplicates?

4 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 12 May 2010, 01:03 PM
Hello Rieni De Rijke,

This is not recommended approach since RadGridView uses virtualization and not all UI rows are available. The best approach is to loop though all data item instead of all UI elements. You can use the Items property of RadGridView to iterate over all data items and check for duplicate property values on those items. You can try something like this:

private void CheckForDuplicateProperties()
{
    foreach (var item in this.playersGrid.Items)
    {
        var myDataItem = (Player) item;
  
        if (this.HasDuplicate(myDataItem))
        {
            // do....
        }
    }
}
  
public bool HasDuplicate(Player player)
{
    var hasDuplicate = false;
  
    foreach (var item in this.playersGrid.Items)
    {
        var myDataItem = (Player) item;
  
        hasDuplicate = myDataItem.Country == player.Country;
    }
  
    return hasDuplicate;
}

Kind regards,
Milan
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Mark
Top achievements
Rank 1
answered on 09 Jul 2010, 04:47 PM
I have a similar need... but only want to check for duplication against the row currently being edited. 

For simplicity, let's say we have a gridview with the columns 'Name', 'City', 'State', and 'Zip'.  I want to insert a new row (or even copy an existing row), but then need to validate that the new row, or any existing row being edited, does NOT match ALL column values.  I can have the same Name, City and State on two rows, but not the same Zip too (as an example).  If a match is found, I want to visually notify the user of such, and set the .IsValid property (in the RowValidating event) to False.

The catch here is that I need to validate against the rows "NewValues" and not the existing or OldValues data.  Make sense?  What is the best way to go about this?

Thanks!
0
Mark
Top achievements
Rank 1
answered on 09 Jul 2010, 05:38 PM
Here's the only thing I can come up with... not sure it's the best approach however (sorry, the project is in VB.NET):

Private Sub gvMyGridView_RowValidating(ByVal sender As System.Object, ByVal e As Telerik.Windows.Controls.GridViewRowValidatingEventArgs) Handles gvMyGridView.RowValidating
         'Does the combined values of this row already exists?
         Dim matchFound As Boolean = False
         Dim s As MyContact = TryCast(e.Row.Item, MyContact)
         Dim ndx As Integer
         Dim test As MyContact
  
         For ndx = 0 To gvMyGridView.Items.Count - 1
            test = TryCast(gvMyGridView.Items(ndx),MyContact)
            'Don't test against the same contact ID (itself)...
            If test.ID <> s.ID Then
               If test.Name = s.Name _
                  AndAlso test.City = s.City _
                  AndAlso test.State = s.State _
                  AndAlso test.Zip = s.Zip Then
  
                  matchFound = True
                  Exit For
               End If
            End If
         Next
  
         If matchFound Then
            e.IsValid = False
         End If
  
      End Sub
0
Milan
Telerik team
answered on 12 Jul 2010, 11:08 AM
Hi Mark,

This approach seems reasonable and it uses data items instead of UI elements which is great.


Best wishes,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Rieni De Rijke
Top achievements
Rank 1
Answers by
Milan
Telerik team
Mark
Top achievements
Rank 1
Share this question
or