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

SelectedRows are not cleared when using Begin and EndUpdate

3 Answers 102 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Arunkumar Dharuman
Top achievements
Rank 1
Arunkumar Dharuman asked on 23 Mar 2011, 01:25 PM
I'm facing a problem while updating the DataTable. The SelectedRows list references the old objects when updating the DataTable using BeginUpdate and EndUpdate.

code snippet is as follows:
gridView.GridElement.BeginUpdate();
DataTable dt = gridView.DataSource as DataTable;
dt.Clear();
 
// Loops a list and add DataRows with dt.NewRow() function
 
gridView.GridElement.EndUpdate();

Upon execution of the above code, the SelectedRows should ideally be cleared. However, they are still having objects in the List. 
I'm not facing this issue when BeginUpdate() and EndUpdate() function is not used. 

I'm using Q1 2010 SP2.

I understand that this is a old version and it might have got resolved in latest versions, however, I'm not in a state to upgrade. Please suggest me some workaround for this.

3 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 25 Mar 2011, 10:24 AM
Hello,

Sorry, i cannot reproduce this issue in the latest version, could you please check if this is happening with the following example:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Telerik.WinControls.Data;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1;
    private List<Person> persons;
    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(radGridView1 = new RadGridView());
        radGridView1.Dock = DockStyle.Fill;
        radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
         radGridView1.ReadOnly = true;
        radGridView1.MultiSelect = true;
        radGridView1.EnableFiltering = true;
 
        persons = new List<Person>();
 
        for (int i = 0; i < 20; i++)
        {
            if (i % 4 == 0)
            {
                persons.Add(new Person
                {
                    Id = i,
                    Name = "L'Person " + i,
                });
            }
            else
            {
                persons.Add(new Person
                {
                    Id = i,
                    Name = "Person " + i,
                });
            }
        }
 
        radGridView1.DataSource = persons;
 
        var button = new RadButton();
        this.Controls.Add(button);
        button.Dock = DockStyle.Bottom;
        button.Text = "Show selected";
        button.Click += new EventHandler(button_Click);
    }
 
    void button_Click(object sender, EventArgs e)
    {
        radGridView1.GridElement.BeginUpdate();
        persons = new List<Person>();
 
        for (int i = 0; i < 30; i++)
        {
            if (i % 4 == 0)
            {
                persons.Add(new Person
                {
                    Id = i,
                    Name = "L'Person " + i,
                });
            }
            else
            {
                persons.Add(new Person
                {
                    Id = i,
                    Name = "Person " + i,
                });
            }
        }
 
        radGridView1.DataSource = persons;
        radGridView1.GridElement.EndUpdate();
        MessageBox.Show(radGridView1.SelectedRows.Count.ToString());
    }
}
 
public class Person
{
    public int Id
    {
        get;
        set;
    }
 
    public string Name
    {
        get;
        set;
    }
 
    public bool Selected
    {
        get;
        set;
    }
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Telerik WinForms MVP
0
Accepted
Alexander
Telerik team
answered on 28 Mar 2011, 11:19 AM
Hello Arunkumar,

I confirm this issue exists in the version of RadGridView you use. We have addressed it in the later releases. As a work-around in your version, you can clear the SelectedRows of the control when the rows of the DataSource are cleared:
this.radGridView1.SelectedRows.Clear();

I hope it helps.

Regards,
Alexander
the Telerik team
0
Arunkumar Dharuman
Top achievements
Rank 1
answered on 29 Mar 2011, 08:00 AM
This worked out. Thanks a lot for your support.

I suppose, 
this.radGridView1.ClearSelection();
also do the same. I've used this code in my program.

Once again thanks for your help.

Regards,
ArunDhaJ

Tags
GridView
Asked by
Arunkumar Dharuman
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Alexander
Telerik team
Arunkumar Dharuman
Top achievements
Rank 1
Share this question
or