Grid - SelectedRows Remove

24 posts, 0 answers
  1. Sangeetha
    Sangeetha avatar
    28 posts
    Member since:
    Oct 2010

    Posted 23 Nov 2010 Link to this post


    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.
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Nov 2010 Link to this post

    Hello,

    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
  3. Sangeetha
    Sangeetha avatar
    28 posts
    Member since:
    Oct 2010

    Posted 23 Nov 2010 Link to this post


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

    }

    }

  4. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Nov 2010 Link to this post

    Hello,

    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
  5. Sangeetha
    Sangeetha avatar
    28 posts
    Member since:
    Oct 2010

    Posted 23 Nov 2010 Link to this post

    BeginUpdate & EndUpdate methods are also not supported.
    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();

  6. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Nov 2010 Link to this post

    Hello,

    Please can you confirm which version you are using?
    Many thanks
    Richard
  7. Sangeetha
    Sangeetha avatar
    28 posts
    Member since:
    Oct 2010

    Posted 23 Nov 2010 Link to this post


    Version: Q1 2010 SP1

  8. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Nov 2010 Link to this post

    Hello,

    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
  9. Sangeetha
    Sangeetha avatar
    28 posts
    Member since:
    Oct 2010

    Posted 23 Nov 2010 Link to this post

    I am sorry it does not work either.
    The For loop exits after first row is removed.
  10. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Nov 2010 Link to this post

    Hello,

    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
  11. Sangeetha
    Sangeetha avatar
    28 posts
    Member since:
    Oct 2010

    Posted 23 Nov 2010 Link to this post

    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.

  12. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Nov 2010 Link to this post

    Hello,

    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
  13. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 23 Nov 2010 Link to this post

    Hello guys,

    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
  14. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Nov 2010 Link to this post

    Hi Emanuel,

    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
  15. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 23 Nov 2010 Link to this post

    Hello again,

    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
  16. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Nov 2010 Link to this post

    Hi Emanuel,

    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
  17. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 23 Nov 2010 Link to this post

    Hello 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
  18. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Nov 2010 Link to this post

    Hi Emanuel,

    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
  19. Hodaya
    Hodaya avatar
    4 posts
    Member since:
    Nov 2017

    Posted 27 Nov in reply to Richard Slade Link to this post

    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?

  20. Dess
    Admin
    Dess avatar
    2222 posts

    Posted 27 Nov Link to this post

    Hello, Hodaya,

    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
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  21. Hodaya
    Hodaya avatar
    4 posts
    Member since:
    Nov 2017

    Posted 27 Nov in reply to Dess Link to this post

    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. 

  22. Dess
    Admin
    Dess avatar
    2222 posts

    Posted 27 Nov Link to this post

    Hello, Hodaya,    

    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
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  23. Hodaya
    Hodaya avatar
    4 posts
    Member since:
    Nov 2017

    Posted 27 Nov in reply to Dess Link to this post

    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...

  24. Dess
    Admin
    Dess avatar
    2222 posts

    Posted 27 Nov Link to this post

    Hello, Hodaya,    

    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
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top