Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in wpf datagrid how to get the blank row on top?

Tags:

wpf

datagrid

in wpf datagrid how to get the blank row on top? ie when user wants to add new row, it always comes at the bottom. But I want it to be on top ... can i do this in XAML ?

like image 200
kedar Avatar asked Feb 22 '10 21:02

kedar


2 Answers

What about NewItemPlaceholderPosition.AtBeginning? I don't have a code example but that seems to be what you're describing. You could always do what Johan is suggesting and Move or Sort the items in the grid programmatically.

Code example added by Ray Burns:

var view = CollectionView.GetDefaultCollectionView(EmployeeList)
             as IEditableCollectionView;
if(view!=null)
  view.NewItemPlaceholderPosition = NewItemPlaceholderPosition.AtBeginning;

Note that this requires NET Framework 3.5 or above.

like image 131
woodyiii Avatar answered Nov 15 '22 07:11

woodyiii


If you're using an MMVM approach, you can add a new row programatically like:

        var newEmp = new EmployeeViewModel(new EmployeeDto());
        EmployeeList.Add(newEmp);
        EmployeeList.Move(EmployeeList.IndexOf(newEmp), 0);

In my example i am using an EmployeeListViewModel to display an ObservableCollection of EmployeeViewModels.

Then you are also able to write tests for this behaviour. More control than in XAML....

/Johan

like image 45
Johan Zell Avatar answered Nov 15 '22 07:11

Johan Zell