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.
Unfortunately, activation email could not send to your email. Please try again.

GridControl Remove Selected Rows

Thread ID:

Created:

Updated:

Platform:

Replies:

95206 Jun 25,2010 11:36 AM Sep 14,2018 05:09 AM Windows Forms 8
loading
Tags: GridControl
Nathaniel Felt
Asked On June 25, 2010 11:36 AM

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

Any help is appreciated.

Jisha Joy [Syncfusion]
Replied On June 28, 2010 03:09 AM

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

Nathaniel Felt
Replied On July 12, 2010 01:20 PM

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.

Nathaniel Felt
Replied On July 12, 2010 01:32 PM

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

Jisha Joy [Syncfusion]
Replied On July 13, 2010 01:55 AM

Hi Nathaniel,

Thank you for your update.

Regards,
Jisha

Adam Bruss
Replied On June 15, 2016 08:23 AM

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.

Amresh S [Syncfusion]
Replied On June 16, 2016 05:17 AM

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.

Mark
Replied On September 13, 2018 03:01 PM

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);
            }



Mohanraj Gunasekaran [Syncfusion]
Replied On September 14, 2018 05:09 AM

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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;