Monday, January 16, 2012

Situation a Silverlight DataGrid to a given XML information source

DataGrid could also be limited to a continued type of datasoures, such as XML resources or something spectial such as RSS nourishes. To situation a given DataGrid to a given xml information file collection, the following example is offered to show you that.


Create a new Silverlight application




Figure 1

First, In the XAML code editor add the following code

  <Grid x:Name="LayoutRoot" Background="White">            <data:DataGrid x:Name="myDataGrid"                     AutoGenerateColumns="True"                     >            data:DataGrid>        Grid>

As you can remak, the DataGrid should be known as so that it could be refered later in the C# value behind. Now, let consider this xml information file as a major collection for our DataGrid.

xml version="1.0" encoding="utf-8" ?><myFamily>
  <
item>
    <
FirstName>BejaouiFirstName>    <LastName>HabibLastName>    <Age>66Age>    <IsMale>trueIsMale>  item>
  <
item>
    <
FirstName>Ben GargaFirstName>    <LastName>KadijaLastName>    <Age>63Age>    <IsMale>falseIsMale>  item>
  <item>
    <
FirstName>BejaouiFirstName>    <LastName>BechirLastName>    <Age>30Age>    <IsMale>trueIsMale>  item>
  <
item>
    <
FirstName>BejaouiFirstName>    <LastName>ArbiaLastName>    <Age>25Age>    <IsMale>falseIsMale>  item>
  <
item>
    <
FirstName>BejaouiFirstName>    <LastName>RimLastName>    <Age>20Age>    <IsMale>falseIsMale>  item>
myFamily>




Second, a category to suit the xml framework should be designed. It could be showed as under


  public class Person        {
            public string FirstName { getset; }
            public string LastName { getset; }
            public string Age { getset; }
            public string IsMale { getset; }
        }


Third, a reference to the System.Xml.Linq namespace should be added


Figure 2

In the code behind, a DataGrid object should be declared and the under code should be implemented

public partial class Page : UserControl        {
            DataGrid oGrid;
            public Page()
            {
                InitializeComponent();
                InitializeGrid();
            }
            public void InitializeGrid()
            {
                XDocument oDoc = XDocument.Load("File.xml");
                var myData = from info in oDoc.Descendants("item")
                             select new Person                             {
                                 FirstName = Convert.ToString(info.Element("FirstName").Value),
                                 LastName = Convert.ToString(info.Element("LastName").Value),
                                 Age = Convert.ToString(info.Element("Age").Value),
                                 IsMale = Convert.ToString(info.Element("IsMale").Value)
                             };
                oGrid = this.FindName("myDataGrid"as DataGrid;
                oGrid.ItemsSource = myData;
            }
        }


This leads to the following result






SPONSORS:

No comments:

Post a Comment

Post a Comment