Hello,
My problem deals with trying to get dates to sort chronologically by clicking on a column header. I feel it has to do with how the data is bound to the grid as other grids in this project do not have this same problem. The other grids only have one list of class objects being bound to them. The grid with the problem has two classes being added to a list of objects, List<object>, and then binding the object list to the grid. Here's some psuedo code and actual code of what I am doing.
I set up the grid like this:
this.DisplayLayout.Bands[0].Columns.Add("DateMod", "Date"); this.DisplayLayout.Bands[0].Columns["DateMod"].Format = WindentConstant.SHORTDATEFORMAT; this.DisplayLayout.Bands[0].Columns["DateMod"].CellDisplayStyle = Infragistics.Win.UltraWinGrid.CellDisplayStyle.FormattedText; this.DisplayLayout.Bands[0].Columns["DateMod"].DataType = typeof(DateTime);
I bind the data to the grid like this:
List<DocumentData> myDocumentData; (assume this is already bound from a db)
List<NoteData> myNoteData; (assume this is already bound from a db)
List<object> mySourceObjects = new;
foreach (DocumentData in myDocumentData)
mySourceObjects.Add(DocumentData);
foreach (NoteData in myNoteData)
mySourceObjects.Add(NoteData);
SetDataBinding(mySourceObjects, null, true);
In the InitializeRow method:
if (e.Row.ListObject.GetType() == typeof(DocumentData))
e.Row.Cells["DateMod"].Value = ((DocumentData)e.Row.ListObject).DateCreate;
else
e.Row.Cells["DateMod"].Value = ((NotesData)e.Row.ListObject).DateCreate;
(DateCreate, for both objects, is a DateTime property.)
So the values show up properly in the grid, but the sort on the DateMod column will not sort the values in chronological order. I wish to fix this sort problem without implementing another class to combine the DocumentData and NoteData classes. Am I missing some properties that need to be set in the grid? Thanks in advance.
Yes, both classes have a DateCreate property that is of type DateTime. :)
Thanks again for your help.
Actually, now that you mention it, I'm not entirely certain how this can possible work at all. If the column's DataType is DateTime, then I don't see how it can be accepting values that are not DateTime values. Unless maybe the two classes you are using are derived from DateTime? If that's the case, then it all makes sense. By setting the Style for FormattedText you were basically telling the column to sort by text, rather than by value.
Actually, I removed the line you suggested:
windentdev said:this.DisplayLayout.Bands[0].Columns["DateMod"].CellDisplayStyle = Infragistics.Win.UltraWinGrid.CellDisplayStyle.FormattedText;
This seems to have actually fixed the problem. I thought this line was needed for formatting the date into the short date format (without the time portion). :)
If you have time would you mind explaining what was going on in the background? Thank you for your help.
Thank you for the quick reply.
Could you help clarify one of the column's properties for me?
windentdev said:this.DisplayLayout.Bands[0].Columns["DateMod"].DataType = typeof(DateTime);
I was under the impression that this line would help the column understand how to sort itself. Is this not true? I thought this would work with the initialize row method setting the DateMod column's values as DateTimes. If this isn't true, I'll go down the IComparable route. :)
Thanks again.
Hi,
There's a couple of things I noticed here.
This line of code doesn't make sense. I doubt this has anything to do with the the problem, and setting the DataType on teh column is probably overriding this, anyway, but just so you know, FormattedText is like RichText. It has nothing to do with formatting dates, so you should get rid of it. :)
Now on to the actual issue...
It appears to me that your DateMod column is of type object and you are populating it with two different types of objects - NotesData and DocumentData. These are objects and not dates and therefore the grid cannot possibly know how to sort or compare them.
So I think you have a couple of choices here. One option would be to implement IComparable on each of these two classes. The IComparable implementation for each one would have to know about the other class and how to get a date from it, then compare that value to it's own date.
Another option would to create an IComparer class which knows about both types and how to get the dates from each one and then returns a comparison of the two dates. You would assign this IComparer to the SortComparer property of the column.