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