datagrid: adding new items

Nov 10, 2008 at 10:22 AM
Hi

I have a problem regarding the addition of new items in my datagrid:
- the datagrid is bound to an observablecollection (mltData) using this code (RESET CODE)
         mltData = Nothing : mltData = moAction.GetList
         moSrc.Source = mltData
         moSrc.SortDescriptions.Add(New SortDescription("UserGrpName", ListSortDirection.Ascending))
         dgGrid.DataContext = moSrc.View
 - As you can see the grid should always be sorted using the property "UserGrpName"

The RESET CODE is run before the datagrid can be edited.

I have some button code to add new entries where I set some defaults (ADD CODE):
      Dim oItem As New BE.BEUserGroup
      oItem.UserGrpName = "<Name>"
      Call mltData.Add(oItem)

When I enter new items using the ADD CODE above all works fine, new items are placed as first datagrid row.

When I run the RESET CODE again before the ADD CODE, new entries will be queued on the bottom of the datagrid.

Any ideas
Regards
Klaus
 
Coordinator
Nov 12, 2008 at 1:23 PM

Instead of updating the view on refresh.  Set the DataGrid's ItemsSource directly.  

moData = GetData()
dgTest.ItemsSource = moData
If dgTest.Items.SortDescriptions.Count > 0 Then dgTest.Items.SortDescriptions.Clear()
dgTest.Items.SortDescriptions.Add(New SortDescription("Desc", ListSortDirection.Ascending))

Nov 12, 2008 at 1:28 PM
Vincent,
thanks for that, works.
Regards
Klaus

From: vinsibal [mailto:notifications@codeplex.com]
Sent: Mittwoch, 12. November 2008 15:23
To: Klaus Wiesel
Subject: Re: datagrid: adding new items [wpf:39575]

From: vinsibal

Instead of updating the view on refresh. Set the DataGrid's ItemsSource directly.

moData = GetData()
dgTest.ItemsSource = moData
If dgTest.Items.SortDescriptions.Count > 0 Then dgTest.Items.SortDescriptions.Clear()
dgTest.Items.SortDescriptions.Add(New SortDescription("Desc", ListSortDirection.Ascending))