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

Selecting GroupRows

4 Answers 176 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ryan
Top achievements
Rank 1
Ryan asked on 25 Apr 2016, 10:06 PM

Hello,

Let me start with what my overall goal is, just in case I'm taking the completely wrong approach to this:
I have multiselect turned on on my RadGridView. I would like to allow a user to quickly select all the ChildRows of a group by just clicking on that specific GroupRow. I would also like to allow them to select multiple groups at the same time by holding shift or ctrl. This could be a combination of ChildRows and GroupRows.

 

Here's what I've tried doing:
1. Using the SelectionChanged event. When I select a GroupRow, the SelectedRows.Count property of my RadGridView is 0 (ie the IsSelected property of the GroupRow that I just selected is still false). I could use the CurrentRow property and just set all of the ChildRows of the current GroupRow to selected, but if the user holds down shift and selects many GroupRows at once, this will only catch the last one.

2. Using the CurrentRowChanged event. I'm not able to use this for the same reason as above: I can only select the ChildRows of the GroupRow that is last in the selection list.

3. Using the SelectionChanging event. Surprisingly, only a single row exists in e.Rows, despite me selecting multiple GroupRows.

 

So I'm left with CurrentRowChanging. With this event, I'm given the OldRow and the NewRow, so it is (probably) possible to determine if the user is holding down ctrl or shift, determine whether the user has filtered or sorted the rows, and then manually select each ChildRow under each GroupRow based on this logic, but that just seems a little obscure and unnecessary.

 

Here is my code (there's nothing to it)... I tried programatically selecting the 2nd group row as well, but the property will not set:

private void RadForm1_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
 
    dt.Columns.Add("col1", typeof(string));
    dt.Columns.Add("col2", typeof(int));
 
    dt.Rows.Add("group1", 0);
    dt.Rows.Add("group1", 1);
    dt.Rows.Add("group1", 2);
    dt.Rows.Add("group1", 3);
    dt.Rows.Add("group2", 0);
    dt.Rows.Add("group2", 1);
    dt.Rows.Add("group3", 0);
    dt.Rows.Add("group3", 1);
    dt.Rows.Add("group4", 0);
    dt.Rows.Add("group5", 1);
    dt.Rows.Add("group6", 0);
    dt.Rows.Add("group7", 1);
 
    radGridView1.DataSource = dt;
    radGridView1.GroupDescriptors.Add(new Telerik.WinControls.UI.GridGroupByExpression("col1 GROUP BY col1"));
 
    radGridView1.Groups[1].GroupRow.IsSelected = true;
}

 

Thank you!

4 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 26 Apr 2016, 11:49 AM
Hello Ryan,

Thank you for writing.

Please note that only data rows can be selected, the group row can be current only. In addition, when a single row is clicked the other selection is cleared. So to handle this properly you need to disable the rows deselection. This can be achieved by using the SelectionChanging event. For example, you can select all child rows when a group row is clicked like this:
bool canChange = false;
 
private void RadGridView1_SelectionChanging(object sender, GridViewSelectionCancelEventArgs e)
{
    if (!canChange)
    {
        e.Cancel = true;
    }
}
 
private void RadGridView1_CurrentRowChanging(object sender, Telerik.WinControls.UI.CurrentRowChangingEventArgs e)
{
    if (e.NewRow is GridViewGroupRowInfo)
    {
        var row = e.NewRow as GridViewGroupRowInfo;
        canChange = true;
        foreach (var item in row.ChildRows)
        {
            item.IsSelected = true;
        }
        canChange = false;
    }
}

This approach will allow you to change the selection only when specific criteria is met. For example, you can use the Click event to unselect particular rows: 
private void RadGridView1_Click(object sender, EventArgs e)
{
    var cell  = radGridView1.ElementTree.GetElementAtPoint(((MouseEventArgs)e).Location) as GridCellElement;
    if (cell != null)
    {
        var row = cell.RowInfo;
        if (row.IsSelected)
        {
            canChange = true;
            row.IsSelected = false;
            canChange = false;
        }
    }
}

I hope this helps. Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Ryan
Top achievements
Rank 1
answered on 29 Apr 2016, 05:44 PM

Dimitar,

Thanks for the reply. I have used the Click event and the GridView is doing almost everything I need it to at this point. I believe the final issue that I'm having is modifying the color of the GroupRow to make it look selected. I am attempting to do this in the ViewCellFormatting event, but it appears that this event will not fire for the current GroupRow until the current row has changed. This also causes some issues with multiselect, considering the event will only fire on the first & last rows.

Is it possible to either force this event to fire for a particular row, or update the row's BackColor another way? I have tried setting GroupRow.Cells[i].Style.BackColor, but it does not appear to work for non data rows.

Thank you!

0
Ryan
Top achievements
Rank 1
answered on 29 Apr 2016, 05:52 PM
I spoke too soon. You can just call rgvPlantsSystems.TableElement.Update(GridUINotifyAction.StateChanged) to force ViewCellFormatting to fire. I think you can even pass a list of rows if you only want to fire it on specific rows.
0
Dimitar
Telerik team
answered on 02 May 2016, 10:41 AM
Hello Ryan,

Your solution is correct and indeed you need to call the Update method in this case. If you have any other questions, please do not hesitate to contact us.
 
Regards,
Dimitar
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
GridView
Asked by
Ryan
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Ryan
Top achievements
Rank 1
Share this question
or