Loop through Radgridview

1 Answer 23 Views
GridView
N
Top achievements
Rank 1
N asked on 28 Nov 2021, 03:19 PM

Hi!

I would appreciate if someone can help me to know how can I loop through radgridview rows? 

because I want to compare the values of newly added row with previous ones ( to avoid duplicates).

 

Also, I have  <telerik:GridViewCheckBoxColumn>. How can I allow one checkbox to be checked ONLY in the whole radgridview.

 

thanks, 

1 Answer, 1 is accepted

Sort by
0
Accepted
Stenly
Telerik team
answered on 01 Dec 2021, 05:14 PM | edited on 02 Dec 2021, 02:36 PM

Hello N,

Due to containers' recycling mechanisms of the UI Virtualization present in the RadGridView control, only the elements in the current viewport are accessible. However, working directly with them will cause inconsistent behavior on your end. Instead, you should work with the underlying data items.

With that said, for example, you could subscribe to the RowValidating event and get the DataContext of the grid view control. In the event, you could prevent adding a new entry if it matches any of the items present in the bound collection. Setting the e.IsValid property to False will cause the row to turn red until either the insertion is canceled or the row's values are changed to valid ones. The following code snippet is an example implementation of this logic:

private void gridView_RowValidating(object sender, GridViewRowValidatingEventArgs e)
{
    var vm = (MyViewModel)(this.gridView.DataContext);
    if (e.EditOperationType == Telerik.Windows.Controls.GridView.GridViewEditOperationType.Insert)
    {
        var club = (Club)e.Row.Item;
        var count = vm.Clubs
                      .Where(x => x.Name == club.Name && x.Established == club.Established && x.StadiumCapacity == club.StadiumCapacity)
                      .ToList()
                      .Count;

        if (count >= 2)
        {
	    //setting this property to false will prevent of being added
            e.IsValid = false;
        }
    }
}

Regarding the second part of the question, there is no straightforward approach to achieve this behavior. However, you could still fulfill this requirement, by implementing custom logic that tracks the current state of the checkbox and changes the rest of the boxes in the collection. I have prepared a sample project that shows an example approach to achieving the wanted result.

I hope the provided information is of help to you.

Regards,
Stenly
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Stenly
Telerik team
commented on 02 Dec 2021, 03:06 PM

I've updated the information regarding the second part of the question (only one checkbox to be checked).
Tags
GridView
Asked by
N
Top achievements
Rank 1
Answers by
Stenly
Telerik team
Share this question
or