|
Binding datagrid to XML document -- rhaazy --
Using C# and VS2003 <?xml version="1.0" encoding="utf-8" ?> <AppSettings> <Datebase> <Server ID="1.0">DSS-SERVER3</Server> <Database ID="1.1">ClientScanTest</Database> <Description ID="1.2">This is ClientScanTest database fools!</Description> </Datebase> </AppSettings> I have an xml file that looks like this. I also have a datagrid. I would like the columns of the datagrid to be named Server, Database, and Description. Then I would like all the values (in this case one of each) associated to be placed under them. I am having some trouble figuring this out, this is what I have so far... FileStream fs = new FileStream("appsettings.xml", FileMode.Open, FileAccess.Read,FileShare.ReadWrite); StreamReader sr = new StreamReader(fs); DataSet ds = new DataSet(); ds.ReadXml(sr); fs.Close(); DataView Source = new DataView(ds.Tables ); ((frmMDIMain)this.ParentForm).dataGrid1.DataSource = Source; |
|
-- Cerebrus --
Why do you have to use a DataView in this case ? Is it part of your requirements ? If not, simply using a Datagrid will suffice. Here's a v. simple example of how to do it : (It's in VB.NET... I'm too lazy to convert it) Note : dgDetail is the name of the Datagrid. Book is the root element of the xml file. --------------------------- Dim ds as New DataSet() ds.ReadXml("..\Xml Files\Books.xml") 'Display the resulting Dataset dgDetail.DataSource = ds dgDetail.DataMember = "Book" --------------------------- |
|
-- rhaazy --
This is what I got now DataSet ds = new DataSet(); ds.ReadXml("appsettings.xml"); ((frmMDIMain)this.ParentForm).dataGrid1.DataSource = ds; ((frmMDIMain)this.ParentForm).dataGrid1.DataMember = "AppSettings"; however I get this error messsage."cannot create a child list for field AppSettings" |
|
-- Cerebrus --
Sorry, my mistake. "Book" in my example is not the root element, but the element that repeats. So, in your case, you should set the Datamember to be "Datebase". When you do this, still it will not give the desired format. Your datagrid will always show the XML file in the same format as when you display an XML file using the "Data" tab in Visual Studio. To display as you desire, you will need to remove the attributes of the 3 fields : Server, Database and Description. |