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

Last row always selected and the first cell in edit mode

1 Answer 61 Views
GridView
This is a migrated thread and some comments may be shown as answers.
masha reznik
Top achievements
Rank 1
masha reznik asked on 15 Jan 2010, 08:48 PM
Hi,
i need that my last row in grid will be always selected and that the first cell in this row will be in edit mode.
BeginEdit() doesnt work for me becouse my ItemSource is not obserableCollection, its a custom collection:
 public interface ICustomObservableCollection<T> : ICollection<T>, INotifyCollectionChanged, INotifyPropertyChanged  
        where T : EntityBase2, IEntity2  
    { }  
 
    public class CustomObservableCollection<T> : ICustomObservableCollection<T> 
        where T : EntityBase2, IEntity2  
{} 


Thanx

Masha

1 Answer, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 18 Jan 2010, 12:38 PM
Hi masha reznik,

I guess you are referring to the BeginInsert method since the BeginEdit is independent of the data source type. In case you are indeed referring to BeginInsert method could you please take a look at the AddingNewdataItem method in our documentation .

In case you are referring to the BeginEdit method here is one possible implementation of automatic selection and editing of the last row:

public Page()
{
    InitializeComponent();
  
    RadGridView1.ItemsSource = new MyBusinessObjects().GetData(20);
  
    this.RadGridView1.Items.CollectionChanged += new NotifyCollectionChangedEventHandler(Items_CollectionChanged);
    this.RadGridView1.DataLoaded += new EventHandler<EventArgs>(RadGridView1_DataLoaded);
    this.RadGridView1.SelectionChanged += new EventHandler<SelectionChangeEventArgs>(RadGridView1_SelectionChanged);
}
  
private void RadGridView1_SelectionChanged(object sender, SelectionChangeEventArgs e)
{
    if (e.AddedItems.Count == 1)
    {
        this.EditRow(e.AddedItems[0]);
    }
}
  
private void RadGridView1_DataLoaded(object sender, EventArgs e)
{
    this.RadGridView1.Dispatcher.BeginInvoke(new Action(
        () =>
        {
            this.SelectLastRow();
            this.EditRow(this.RadGridView1.SelectedItem);
        }));
}
  
private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    this.SelectLastRow();
}
  
private void SelectLastRow()
{
    if (this.RadGridView1.Items.Count <= 0)
        return;
  
    this.RadGridView1.SelectedItem = this.RadGridView1.Items[this.RadGridView1.Items.Count - 1];
    this.RadGridView1.CurrentItem = this.RadGridView1.SelectedItem;
}
  
private void EditRow(object rowItem)
{
    if (rowItem == null)
        return;
  
    var rowToEdit = this.RadGridView1.ItemsGenerator.ContainerFromItem(rowItem) as GridViewRow;
  
    if(rowToEdit != null)
        rowToEdit.BeginEdit();
}

Best wishes,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
masha reznik
Top achievements
Rank 1
Answers by
Milan
Telerik team
Share this question
or