Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF DataGrid
Hello.
I'm using DataGrid to edit and display a list of my objects as ItemsSource, List<TimeSegment>, and my object looks like this:
public class TimeSegment
    {
        
        public DateTime StartTime
        public DateTime EndTime
        public TimeSpan Span
        {
            get
            {
                return this.EndTime - this.StartTime;
            }
        }
    }
}
 
The Span property is calculated.
 
But the Span column did not update automatically, after I edit the StartTime or EndTime in DataGrid. And I want to sort the list after editing too.
 
I have a work around about this, but it dose not solve this gracefully.
 
Here is my work around, first I set a flag in the RowEditEnding event, then I ClearBinding for ItemsSourceProperty , sort the list and rebind the property in the CurrentCellChanged event after checking the flag.
 
My first question is "Is there a better way to do this?".
 
If I have to use this work around, then I got another problem.
 
I want to do some logic after the List<TimeSegment> changed. It works fine after editing, but not with deleting row(s).
 
My second question is "How can I do with deleting row(s)?".
 
I tried putting the logic in the event handler of Executed of DataGrid.DeleteCommand, but this will override the default action of DataGrid, the row did not deleted, my logic get executed.
 
<DataGrid.CommandBindings>
   <CommandBinding Command="DataGrid.DeleteCommand" 
                   Executed="CommandBinding_Executed" />
</DataGrid.CommandBindings>
 
Can somebody help me? Thanks in advance.
Posted 19-Nov-12 3:57am
Comments
n.podbielski at 19-Nov-12 13:06pm
   
Why you just call DataGrid.Update or DataBind method to just update contents of whole table.
sun.bolin at 19-Nov-12 22:58pm
   
Thanks for your reply.
 
But I can't find a update method in DataGrid in wpf. And if there is one, where should I put it in? Also in the CurrentCellChanged event handler after checking my needUpdate flag?
 
If so, my second question dose not solved. thanks.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

HI implement INotifiyPropertyChange in your class and bind your datagrid with an id with an ObservableCollection<TimeSegment>; object
  Permalink  
Comments
sun.bolin at 19-Nov-12 23:29pm
   
Thanks Jean, your solution should work for my situation. I may have to implement the interface and use a ObservableCollection instead, if I need an automatic two way binding facility.
 
Because of the class with a List property and the TimeSegment are business classes, so I don't want them have too much dependency to the none business stuff at first.
 
Both INotifiyPropertyChange and ObservableCollection are related to some kind of UI tech or property driven system. So my business classes are kind of not pure anymore.
 
What if I don't own these classes? Should I wrap them or inherit them with change notification capability?
 
Anyway I will accept your solution for now, thank you for help.
Jean-Claude ADIBA at 20-Nov-12 18:55pm
   
Thanks for trying my solution. you know the binding system of WPF take sense with these classes because without them you will use WPF like any winform application.
I agree with you when you want to build your business classes as POCO without any of these kind of classes. To go further more you must build your application with MVVM architecture to take advantage of WPF binding. you must wrap your List object in ObservableCollection object in a viewmodel which will bind to your UI. follow this link to have more information about the MVVM pattern
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
regards
sun.bolin at 22-Nov-12 8:51am
   
Hi, Jean. I have one more question.
I have implemented INotifyPropertyChanged, and use a ObservableCollection instead of List, the DataGrid works fine.
When I add a listener to ObservableCollection.CollectionChanged event, the listener was just invoked after the collection adding or removing item from it, but not after editing a property of an item in it.
Should this be the way they act?
If so, how dose DataGrid aware of the editing of an item's property in a ObservableCollection.
Thanks a lot.
Jean-Claude ADIBA at 23-Nov-12 12:35pm
   
you cannot to be notified when an item change because ObservableCollection collection only fires the CollectionChangedEvent.
Because you implement INotifyPropertyChanged, you will see changes to the items in the view (WPF does this automatically if you ), but if you need to execute manual actions when an item changes, you can use BindingList and suscribe to BindingList.ListChanged event. with ListChangedEventArgs ( ListChangedEventHandler's proprety) you can manupulate your data in easy way.
 
Also you can always use ObservableCollection Class and handle datagrid CellEditEnding event like this:
 
private void dg_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (e.Column == yourColumn)
{
if (e.EditAction == DataGridEditAction.Commit)
{
 
TextBox t = e.EditingElement as TextBox; // Assumes columns are all TextBoxes
 
yourObject = e.Row.Item; //you must cast
yourObject.Property1 = t.Text ; // the value of the cell
}
}
}
sun.bolin at 25-Nov-12 7:21am
   
Thanks for answering my question so thoroughly. It helps me a lot.
 
I feel welcome here, this must be a very good forum.
 
And I hope I can be a friend of yours. :)
Jean-Claude ADIBA at 25-Nov-12 7:38am
   
no matter you are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 554
1 Gihan Liyanage 338
2 RyanDev 248
3 Sergey Alexandrovich Kryukov 243
4 ChauhanAjay 220
0 Sergey Alexandrovich Kryukov 9,011
1 OriginalGriff 7,941
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100