Linq to XML binding to DataGrid

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am attempting to bind XML data to a silverlight DataGrid via Linq. Included in that data is a column of dates (birth dates) that may be empty. My data item structure uses a nullable DateTime object to store the birthdate. The birthdate
in the XML is either . if the date is unknown, or in the format 02MAR71. I have the basic binding working as desired (data is loaded into the grid), but some of the refinements that Id like to add are giving me trouble...
I have several issues that I am struggling to solve:
1. How to I set a format on the birthdate column to something that doesnt include the time? I considered creating my own time (Date) and providing an override for ToString() that provides the string in the proper format. If I do that,
I also have to write code to provide sorting. If I can stay with a DateTime object and simply set a format, the sorting just works.
2. How do I add a grouping clause? Id like to be able to Company or Region or whatever, based on a user preference.
3. How do I provide a custom sort order for the Rank column? Since the DataGrid considers that column strings, the sort results in alphabetical order. I really want them to be listed according to rank order.
4. Eventually, I plan on adding custom column ordering. I plan on displaying the DataGrid the very first time with a default set of columns in a predetermined order, but provide the ability for the user to reorder and hide/show different columns.
Once the user has things the way they want, I want to allow them to save that layout such that the next time the DataGrid initializes with the right columns ordered as they wanted. Is Linq going to provide enough flexibility to allow this type of functionality
or amy I just going to have to load all columns in the default order and then reorder and hide/unhide the columns as needed? (If thats the way to go, I shouldnt have trouble doing that as a second phase of InitializeGrid.)
Heres the current code:
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public <span style="color:Blue; void InitializeGrid()
{
XDocument xdoc = XDocument.Load( <span style="color:#A31515; "SampleData.xml" );
<span style="color:Blue; var myData = <span style="color:Blue; from info <span style="color:Blue; in xdoc.Descendants( <span style="color:#A31515; "People" )

<span style="color:Blue; select <span style="color:Blue; new Person
{
Name = info.Attribute( <span style="color:#A31515; "name" ).Value,
Company = info.Attribute( <span style="color:#A31515; "company" ).Value,
Birthdate = ParseDate( info.Attribute( <span style="color:#A31515; "birthdate" ).Value,
Rank = info.Attribute( <span style="color:#A31515; "rank" ).Value,
Region = info.Attribute( <span style="color:#A31515; "region" ).Value
};
dataGrid.ItemsSource = myData;
}

<span style="color:Blue; private DateTime? ParseDate( <span style="color:Blue; string pre )
{
<span style="color:Blue; if ( pre.Equals( <span style="color:#A31515; "." ) )
<span style="color:Blue; return <span style="color:Blue; null;
<span style="color:Blue; else
<span style="color:Blue; return DateTime.ParseExact( pre, <span style="color:#A31515; "ddMMMyy", CultureInfo.InvariantCulture );
}
[/code]


View the full article
 
Back
Top