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

Delete all rows in a group

3 Answers 128 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Nadia Sangiovanni
Top achievements
Rank 1
Nadia Sangiovanni asked on 09 Jul 2012, 02:33 PM
Hi support,

Is it possible to select a group, right-click or press the delete key, and all the lines in that group will be deleted?

Example:
GROUP1
    SUBGROUP A
        SUBSUBGROUP i
            row i1
            row i2
            row i3
        SUBGROUP ii
            row ii1
            row ii2
            row ii3
    SUBGROUP B
....

The user select SUBSUBGROUP i in the row header and press delete and the rows row i1, row i2, row i3 will be deleted.

Regards,
Nadia

3 Answers, 1 is accepted

Sort by
0
Accepted
Stefan
Telerik team
answered on 12 Jul 2012, 10:35 AM
Hi Nadia,

Thank you for writing.

In order to delete the rows upon pressing the Delete key, you need to subscribe to the KeyDown event of RadGridView and check if the pressed key is the Delete key and if the CurrentRow of RadGridView is a group row. If both conditions are satisfied, iterate over the ChildRows collection and delete its rows. 

In regards to the context menu, this can be achieved by using the MouseDown event of RadGridView and if the clicked cell is a group cell open your own context menu with an item, which will delete the rows upon click.

Both approaches are implemented in the attached project. Feel free to give it a try.

Should you have any other questions, do not hesitate to contact us.

All the best,
Stefan
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Nadia Sangiovanni
Top achievements
Rank 1
answered on 12 Jul 2012, 02:19 PM
Wonderful Stefan!

That was exactly what I was looking for.

For others users, I change a bit your function to work when you delete a group that contains subgroup.
private void DeleteGroupRows(GridViewGroupRowInfo groupRow)
        {            
            if (groupRow != null)
            {
                for (int i = groupRow.ChildRows.Count - 1; i >= 0; i--)
                {
                    if (groupRow.ChildRows[i] is GridViewDataRowInfo)                                            
                        groupRow.ChildRows[i].Delete();
                    else if (groupRow.ChildRows[i] is GridViewGroupRowInfo)
                    {
                        GridViewGroupRowInfo subGroupRow = groupRow.ChildRows[i] as GridViewGroupRowInfo;
                        DeleteGroupRows(subGroupRow);
                    }
                }
            }            
        }

Regards,
Nadia
0
Stefan
Telerik team
answered on 16 Jul 2012, 12:08 PM
Thank you for helping our community. I am sure that someone will benefit from this thread.
 
Kind regards,
Stefan
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
Tags
GridView
Asked by
Nadia Sangiovanni
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Nadia Sangiovanni
Top achievements
Rank 1
Share this question
or