开发者

About XML Data Binding to the Datagridview

开发者 https://www.devze.com 2023-02-13 12:37 出处:网络
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 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;
0

精彩评论

暂无评论...
验证码 换一张
取 消