About XML Data Binding to the Datagridview
I want to bind the xml file to the datagridview control. but i don't want to use the dataset for binding.
. I want to bind the xml file directly to the datagridvi开发者_开发问答ew. IS it possible? If yes pls reply me with some example...
thanx...
One of the ways to accomplish this is by using Linq. First you will need to add a reference to System.XML.Linq, and then use code below:
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;
精彩评论