Hi
I need to remove the selectedRows in a Radgrid. I used the following code:
GridViewSelectedRowsCollection RowCollection = m_radGridSource.SelectedRows;
foreach (GridViewRowInfo GridDataRow in RowCollection)
{
m_radGridSource.Rows.Remove(GridDataRow
as GridViewDataRowInfo);
}
But exception was thrown as enumerator modified.
Please let me know how ro remove selected rows.
Sangeetha.
25 Answers, 1 is accepted

You need to use the new DeferRefresh method (or BeginUpdate/EndUpdate).. for exmaple
Using
Me
.RadGridView1.DeferRefresh()
For
Each
row
As
GridViewRowInfo
In
Me
.RadGridView1.Rows
If
row.Cells(1).Value.ToString() =
"John"
Then
Me
.RadGridView1.Rows.Remove(row)
End
If
Next
End
Using
using
(
this
.RadGridView1.DeferRefresh()) {
foreach
(GridViewRowInfo row
in
this
.RadGridView1.Rows) {
if
(row.Cells(1).Value.ToString() ==
"John"
) {
this
.RadGridView1.Rows.Remove(row);
}
}
}
Richard
EDIT// Edited post to update code sample to show the Using statement should be outside the loop

I am not able to find the mentod DeferRefresh for RadGrid.
Following code also does not work,
foreach
(GridViewDataRowInfo GridDataRow in m_radGridSource.Rows)
{
if (GridDataRow.IsSelected)
{
m_radGridSource.Rows.Remove(GridDataRow);
}
}

If you are using an older version of the controls, you need to use BeginUpdate / EndUpdate. Please try the following code
m_radGridSource.BeginUpdate();
foreach
(GridViewDataRowInfo GridDataRow
in
m_radGridSource.Rows) {
if
(GridDataRow.IsSelected) {
m_radGridSource.Rows.Remove(GridDataRow);
}
}
m_radGridSource.EndUpdate(
true
);
Let me knw if that works for you.
thanks
Richard

Hence I used BeginEdit & EndEdit as follows, which also throws exception:
m_radGridSource.BeginEditMode = Telerik.WinControls.
RadGridViewBeginEditMode.BeginEditProgrammatically;
m_radGridSource.BeginEdit();
foreach (GridViewDataRowInfo GridDataRow in m_radGridSource.SelectedRows)
{
m_radGridSource.Rows.Remove(GridDataRow);
}
m_radGridSource.EndEdit();

Please can you confirm which version you are using?
Many thanks
Richard

Version: Q1 2010 SP1

I am using the latest version, so I am trying to find the information you need and remember what we were doing in this older version.
Please could you see if you can replace this code...
m_radGridSource.BeginUpdate();
foreach
(GridViewDataRowInfo GridDataRow
in
m_radGridSource.Rows) {
if
(GridDataRow.IsSelected) {
m_radGridSource.Rows.Remove(GridDataRow);
}
}
m_radGridSource.EndUpdate(
true
);
With this...
m_radGridSource.GridElement.BeginUpdate();
foreach
(GridViewDataRowInfo GridDataRow
in
m_radGridSource.Rows) {
if
(GridDataRow.IsSelected) {
m_radGridSource.Rows.Remove(GridDataRow);
}
}
m_radGridSource.GridElement.EndUpdate(
true
);
Hope this helps
Richard

The For loop exits after first row is removed.

I am unsure of what you mean by The For loop exits after first row is removed. Please can you expand on this?
Please can you try deleing a single row, wrapping the remove in the
this
.radGridView.GridElement.BeginUpdate();
// remove 1 row
this
.radGridView.GridElement.EndUpdate();
and let me know the result.
thanks
Richard

GridViewRowInfo
[] selectedRows = new GridViewRowInfo[gridSeleted.SelectedRows.Count];
gridSeleted.SelectedRows.CopyTo(selectedRows, 0);
gridSeleted.GridElement.BeginUpdate();
for (int i = 0; i < selectedRows.Length; i++)
{
gridSeleted.Rows.Remove(selectedRows[i]
as GridViewDataRowInfo);
}
gridSeleted.GridElement.EndUpdate();
The above code works.
Thanks to my team mate Srikanth for that.
Thanks for your support, Richard,
Sangeetha.

I dont think you should need to copy the rows.
Please can you try this. I am pretty sure under your version that this will work.
this
.RadGridView1.GridElement.BeginUpdate();
foreach
(GridViewRowInfo row
in
this
.RadGridView1.SelectedRows) {
this
.RadGridView1.Rows.Remove(row);
}
this
.RadGridView1.gridElement.EndUpdate();
richard

You are modifying the value of a collection while iterating it...
Please try the following:
var selectedRows = radGridView1.SelectedRows.ToArray();
foreach
(var row
in
selectedRows)
{
radGridView1.Rows.Remove(row);
}
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
Telerik WinForms MVP

There's no need to copy to an array when looping over SelectedRows. As long as you include BeginUpdate and EndUpdate you can remove the rows.
Me
.RadGridView1.MultiSelect =
True
Me
.RadGridView1.BeginUpdate()
For
Each
row
As
GridViewRowInfo
In
Me
.RadGridView1.SelectedRows
Me
.RadGridView1.Rows.Remove(row)
Next
Me
.RadGridView1.EndUpdate()
Regards,
Richard

Even if it does work, the selected rows property is connected (synchronized) to the Rows property, it could work for 1,2,3 versions and if they decide to change something it will cause some very strange anomalies.
Why should we break the basic rules of oop for something so simple.
In my point of view, the use of a ToArray(), or a reverse loop should be the preferred choice over hoping that telerik won't change the mechanism behind BeginUpdate() and EndUpdate() - which, in my point of view should not stop the synchronization between rows and selected rows.
I actually had this problem and had to perform some extra logic after the EndUpdate() in order to select some other rows because you cannot change the selected rows from within the BeginUpdate / EndUpdate.
Best Regards,
Emanuel Varga
Telerik WinForms MVP

I see your point, but take a look at this forum post. This is exactly the same principal of looping over a collection and making modifcations to it, and it was agreed here by all, including Telerik that Begin/End Update or the more elgant DeferRefresh was the acceptable method for thier controls.
Regards,
Richard

But those are two different things, there you were just iterating over the rows collection.
Here you are using the selected rows collection (which is/should be synchronized with the rows collection) and doing changes to the rows collection, hope you see my concern here, there are a lot of things that could backfire and cause some strange results or worse in some cases.
Honestly i haven't yet taken a look on the synchronization mechanism between the Rows and the SelectedRows collection, but i have to take a look when I'll have some free time.
Best Regards,
Emanuel Varga
Telerik WinForms MVP

Yes, I understand exactly where you are coming from, and without the provided begin/end updates I'd do exactly the same thing. I'd be interested to see any results you find after looking at the sync machanism more closely but perhaps it's a matter of choice. I don't see the other post as so different. Ok, it's the rows collection so there's no synchronization between the rows collection and another collection, but the BeginUpdate method does currently suspend all events (GridViewSynchronizationService.SuspendEvent)
In my view, there are naturally breaking changes in most releases, which is part of the wave of progress, and this would certainly be a breaking change, should telerik decide to change the Begin/End Update functionality in this way. But why create extra objects on the heap (especially if you're doing it often) when this mechanism works perfectly well. I guess the beauty of programming is that there are lots of ways to get to the same result.
Interesting discussion. Thanks for sharing your thoughts.
Regards,
Richard

Hi. I am trying to remove rows and I get "formatException". I've try to remove the rows like that:
grid.GridElement.BeginUpdate();
foreach (GridViewDataRowInfo row in grid.Rows)
{
if (row.Cells[1].Value.ToString() == "False")
{
grid.Rows.Remove(row);
}
}
grid.GridElement.EndUpdate();
AND like that:
grid.GridElement.BeginUpdate();
IEnumerable<GridViewDataRowInfo> uncheckedRows = grid.Rows.Where<GridViewDataRowInfo>(row => row.Cells[1].Value.ToString() == "False");
foreach (GridViewDataRowInfo uncheckedRow in uncheckedRows)
grid.Rows.Remove(uncheckedRow);
grid.GridElement.EndUpdate();
BOTH WAY DOES NOT WORK...
can you help me please?
Thank you for writing.
You can find below is a sample code snippet demonstrating how to delete a collection of rows from RadGridView. Note that if you use a foreach loop for the RadGrid.Rows collection you can't modify the collection itself. That is why it is necessary to store the rows in a separate list and then delete the rows.
public
RadForm1()
{
InitializeComponent();
DataTable dt =
new
DataTable();
dt.Columns.Add(
"Id"
,
typeof
(
int
));
dt.Columns.Add(
"Name"
,
typeof
(
string
));
dt.Columns.Add(
"IsActive"
,
typeof
(
bool
));
for
(
int
i = 0; i < 10; i++)
{
dt.Rows.Add(i,
"Item"
+ i, i % 2 == 0);
}
this
.radGridView1.DataSource = dt;
this
.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
}
private
void
radButton1_Click(
object
sender, EventArgs e)
{
this
.radGridView1.BeginUpdate();
BindingList<GridViewDataRowInfo> rowsToDelete =
new
BindingList<GridViewDataRowInfo>();
foreach
(GridViewDataRowInfo row
in
this
.radGridView1.Rows)
{
if
(row.Cells[2].Value.ToString() ==
"False"
)
{
rowsToDelete.Add(row);
}
}
while
(rowsToDelete.Count > 0)
{
this
.radGridView1.Rows.Remove(rowsToDelete.First());
rowsToDelete.RemoveAt(0);
}
this
.radGridView1.EndUpdate();
}
The attached gif file illustrates the achieved behavior.
I hope this information helps. Should you have further questions I would be glad to help.
Regards,
Dess
Progress Telerik

Hi.
Thank you for respond.
This solution is very similar to my second way and it is not working... I get a "Format" exception when I hit "Remove()" function.
Thank you for writing back.
I have attached my sample project for your reference. It works as expected on my end with the latest version 2017.3.1017. Could you please specify the exact steps how to reproduce the problem? Once we replicate the problem on our end, we can make an adequate analysis of the problem and assist you further. Thank you in advance for the cooperation.
I am looking forward to your reply.
Regards,
Dess
Progress Telerik

I have 3 grids and now I notice that this is work only for 2 grids...
I try to think why this is not work for the first grid.
At first, I thought that this is happened because I change the "CollectionRowIndex" column index, but this is not the reason.
I repeat, I get a "Format" exception, so this is may be something in the rows values...
Thank you for writing back.
What kind of data do you store in the problematic grid? Probably the cell's value can't be converted to a string. You can try to debug the project and trace what is happing. Is the error reproducible in the provided sample project from my previous post?
Feel free to submit s support ticket where you can provide a sample project demonstrating the error you are facing. Thus, Telerik support will gladly assist you.
I am looking forward to your reply.
Regards,
Dess
Progress Telerik

Hi.
I also have a goal to remove a couple of first rows from the RadGridView.
Being inspired by the example I am trying to go with the following code:
this.dgvMsgArea.BeginUpdate();
BindingList<
GridViewRowInfo
> rowsToDelete = new BindingList<
GridViewRowInfo
>();
for ( int i = 0; i <
count
; ++i )
{
rowsToDelete.Add( dgvMsgArea.Rows[i] );
}
while ( rowsToDelete.Count > 0 )
{
if ( rowsToDelete.First().Tag as BuildMessageItem != null )
{
( rowsToDelete.First().Tag as BuildMessageItem ).Row = null;
rowsToDelete.First().Tag = null;
}
this.dgvMsgArea.Rows.Remove( rowsToDelete.First() );
rowsToDelete.RemoveAt( 0 );
}
this.dgvMsgArea.EndUpdate();
But I keep getting exception at the EndUpdate() method line:
An exception of type 'System.InvalidOperationException' occurred in System.dll but was not handled in user code
Additional information: Collection was modified after the enumerator was instantiated.
Stack Trace leads to some collection internals:
at System.Collections.Generic.LinkedList`1.Enumerator.MoveNext()
at Telerik.WinControls.UI.GridViewSynchronizationService.ContainsEvent(Predicate`1 predicate)
at Telerik.WinControls.UI.GridViewTemplate.IsFilteringPerformed()
at Telerik.WinControls.UI.GridViewTemplate.EndCurrentRowUpdate(Boolean resumeService, Boolean update)
at Telerik.WinControls.UI.GridViewTemplate.EndUpdate(Boolean notify, DataViewChangedEventArgs e)
at Telerik.WinControls.UI.MasterGridViewTemplate.EndUpdate(Boolean notify, DataViewChangedEventArgs e)
at Telerik.WinControls.UI.GridViewTemplate.EndUpdate()
at Telerik.WinControls.UI.RadGridView.EndUpdate()
Can you guide me to what is wrong here?
I have attached again my sample project. The error doesn't occur on my end with the latest version. Could you please specify the exact steps how to reproduce the problem with the provided project? Alternatively, you can submit a support ticket where you can give us additional information about the precise case and provide a project demonstrating the error. Thus, we would be able to investigate the precise case and assist you further. Thank you in advance.
I hope this information helps. If you have any additional questions, please let me know.
Regards,
Dess
Progress Telerik