Declined
Last Updated: 15 Oct 2015 10:42 by ADMIN
ADMIN
Dess | Tech Support Engineer, Principal
Created on: 17 Jun 2015 14:28
Category: GridView
Type: Bug Report
1
FIX. RadGridView - incorrect group row is expanded when a new item is inserted to the BindingList data source
BindingList<Item> items = new BindingList<Item>();

public Form1()
{
    InitializeComponent();

    for (int i = 0; i < 20; i++)
    {
        items.Add(new Item(i % 4, "Item" + i, "Type" + i % 2));
    }
    this.radGridView1.DataSource = items;
    this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
}

public class Item
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Type { get; set; }

    public Item(int id, string name, string type)
    {
        this.Id = id;
        this.Name = name;
        this.Type = type;
    }
}

private void radButton1_Click(object sender, EventArgs e)
{
    items.Insert(0, new Item(2, "Test", "Type0"));
}

WORKAROUND I:

GridViewSynchronizationService.SuspendEvent(this.radGridView1.MasterTemplate, KnownEvents.CurrentChanged);
Item item = new Item(2, "Test", "Type0");
items.Insert(0, item);
GridViewSynchronizationService.ResumeEvent(this.radGridView1.MasterTemplate, KnownEvents.CurrentChanged);

foreach (GridViewRowInfo row in this.radGridView1.Rows)
{
    if (row.DataBoundItem == item)
    {
        this.radGridView1.CurrentRow = row;

        break;
    }
}



WORKAROUND II:

this.radGridView1.Rows.CollectionChanged += Rows_CollectionChanged;
this.radGridView1.GroupExpanding+=radGridView1_GroupExpanding;

List<GridViewRowInfo> expandedGroups = new List<GridViewRowInfo>();

private void radGridView1_GroupExpanding(object sender, GroupExpandingEventArgs e)
{
    if (!e.IsExpanded && shouldCollapse)
    {
        expandedGroups.Add(e.DataGroup.GroupRow);
    }
}

bool shouldCollapse = false;

private void radButton1_Click(object sender, EventArgs e)
{
    shouldCollapse = true;
    items.Insert(0, new Item(2, "Test", "Type0"));
}

private void Rows_CollectionChanged(object sender, Telerik.WinControls.Data.NotifyCollectionChangedEventArgs e)
{
    if (e.Action == Telerik.WinControls.Data.NotifyCollectionChangedAction.Add)
    {
        this.radGridView1.CurrentRow = e.NewItems[0] as GridViewRowInfo;
        this.radGridView1.CurrentRow.IsSelected = true;
        GridViewRowInfo parent = this.radGridView1.CurrentRow.Parent as GridViewRowInfo;
        if (parent != null)
        {
            parent.IsExpanded = true;
        }

        if (shouldCollapse)
        {
            shouldCollapse = false;
            foreach (GridViewRowInfo r in expandedGroups)
            {
                if (!ContainsParentRow(parent, r))
                {
                    r.IsExpanded = false;
                }
            }
            expandedGroups.Clear();
        }
    }
}

private bool ContainsParentRow(GridViewRowInfo parent, GridViewRowInfo r)
{
    GridViewGroupRowInfo group = r as GridViewGroupRowInfo;
    if (group != null)
    {
        foreach (GridViewRowInfo childRow in group.ChildRows)
        {
            return ContainsParentRow(parent, childRow);
        }
    }

    return parent == r.Parent;
}
Attached Files:
1 comment
ADMIN
Ivan Petrov
Posted on: 25 Sep 2015 13:30
The behavior of RadGridView is due to the updates it receives from the CurrencyManager of the BindingList (its data source). When an item is inserted into the BindingList at position 0, effectively all items' indices are shifted up to free the position at index 0. At this point, the CurrencyManager fires a PositionChanged event since the current item has its position shifted up. This causes the grid to ensure that the current item is visible and expands the wrong group. After the new item is inserted the position of the CurrencyManager is changed again to the newly added item and the grid again ensures that the item is visible by expanding its group.

To avoid this, you can use either of the workarounds provided in the description.