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
close icon

GridControl Remove Selected Rows

Is there a simple way to remove the selected rows in a GridControl?

Any help is appreciated.

8 Replies

JJ Jisha Joy Syncfusion Team June 28, 2010 07:09 AM UTC

Hi Nathaniel,

You could use the Rows.RemoveRange to remove the selected row ranges in GridControl. See the code:

GridRangeInfo r= this.gridControl1.Selections.Ranges.ActiveRange;

this.gridControl1.Rows.RemoveRange(r.Top - 1, r.Bottom - 1);


Regards,
Jisha


NF Nathaniel Felt July 12, 2010 05:20 PM UTC

Jisha,

Your example works well when only one item is selected or a sequential range is selected, but not if there are multiple non-sequential selections (ie rows 1, 3, and 10).

Do you have a sample that would work for this particular case?

Thank you.


NF Nathaniel Felt July 12, 2010 05:32 PM UTC

Nevermind...

I found the information I needed. For anyone else looking to do something similar:

VB.NET

Dim range as GridRangeInfoList = GridControl1.Selections.Ranges

If range IsNot Nothing Then
For i = (range.Count -1) To 0 Step -1
GridControl1.Rows.RemoveRange(range(i).Top,range(i).Bottom)
Next
End If


JJ Jisha Joy Syncfusion Team July 13, 2010 05:55 AM UTC

Hi Nathaniel,

Thank you for your update.

Regards,
Jisha


AB Adam Bruss June 15, 2016 12:23 PM UTC

The solution in Nathaniel's last comment for removing multiple gridrangeinfos does not work for me. What is the proper way to remove all the GridRangeInfos in a GridRangeInfoList? Removing one gridrangeinfo in the list seems to alter the list so the next time you go through the loop the list is empty.


AS Amresh S Syncfusion Team June 16, 2016 09:17 AM UTC

Hi Adam, 
 
Thank you for using Syncfusion products. 
 
In order to remove a selected row from the grid, you can use the RemoveRange() method via the Rows property. Then, the grid updates the cells with the removed row values. Please make use of the following code snippet to remove the selected range of rows and the attached sample. 
CodeSnippet: 
 
// Trigger the required event. 
this.button1.Click += new System.EventHandler(this.button1_Click); 
 
private void button1_Click(object sender, EventArgs e) 
{ 
     GridRangeInfo ranges = this.gridControl1.Selections.Ranges.ActiveRange; 
     this.gridControl1.Rows.RemoveRange(ranges.Top, ranges.Bottom); 
     this.gridControl1.Refresh(); 
} 
 
Sample: 
 
Regards, 
Amresh S.


MA Mark September 13, 2018 07:01 PM UTC

That solution is still not working for me. I have solved it this way.
By iterating the grid in reverse order.
I first clear the cells, I am iterating through a grid to move selected rows to a second grid and clearing the rows in that foreach loop using: 

this.gridControl1.ClearCells(GridRangeInfo.Row(x), true);

when that loop is done, I do another for loop starting at the largest row value to the first row
In this loop I look for a blank entry, like this:

  for (int i = this.gridControl1.RowCount; i > 0; i--)
            {
                if (gridControl1[i, 2].Text.Length == 0)
                {
                    this.gridControl1.Rows.RemoveRange(i, i);
                }
            }

You cant delete the rows in a for each loop because you get a collections modified error
In the for next loop, when you delete a row the number of rows (RowCount) is reduced by 1
If you did the loop from row 1 to the bottom, deleting row 1 causes row 2 to become row 1
if row 1 now has a length of 0 in the cell your loop skips over it.

For large grids, you could use a list and save the row numbers when clearing cells in the foreach loop
to save time when actually deleting them again going from highest to lowest row

Here is my  foreach loop: It copies grid 1 selections to grid 2, erases cell data without deleting the rows

          int r = NextAvailableRow(); // A method to search for an empty row checks cell length.
          int x = 0;
           foreach (GridRangeInfo range in this.gridControl1.Selections.Ranges)
            {
                Int32.TryParse(range.Info.ToString().Substring(1), out x);
                this.gridControl2[r, 2].Text = this.gridControl1[x, 1].Text;
                this.gridControl1.ClearCells(GridRangeInfo.Row(x), true);
            }




MG Mohanraj Gunasekaran Syncfusion Team September 14, 2018 09:09 AM UTC

Hi Mark, 

Thanks for your update. 
 
Yes, you can delete the rows in reverse order to avoid the collection modified error. Also, you can use the following solution to delete the selected ranges, 

To delete the dynamically selected rows you could use the RemoveRange method with cloneable object of SelectedRanges. Please refer the following code example, 

Code example 
GridRangeInfoList rangeList = this.gridControl1.Model.SelectedRanges.Clone(); 
foreach (GridRangeInfo range in rangeList) 
    this.gridControl1.Rows.RemoveRange(range.Top, range.Bottom); 
 
To avoid the empty row generating while pressing the delete key, you could handle the KeyDown event while pressing the delete key. Please refer the following code example, 

Code example 
this.gridControl1.KeyDown += GridControl1_KeyDown; 
private void GridControl1_KeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.KeyData == Keys.Delete) 
    { 
        GridRangeInfoList rangeList = this.gridControl1.Model.SelectedRanges.Clone(); 
        foreach (GridRangeInfo range in rangeList) 
            this.gridControl1.Rows.RemoveRange(range.Top, range.Bottom); 
        e.Handled = true; 
    } 
} 
 
 
Please let us know if you have any concerns. 

Regards, 
Mohanraj G 


Loader.
Live Chat Icon For mobile
Up arrow icon