WinForms-网格视图绑定到xml文件 [英] WinForms - Grid View bind to xml file

查看:70
本文介绍了WinForms-网格视图绑定到xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我试图(首次)制作一个简单的应用程序,该应用程序将允许我(通过DataSet)将XML文件读入Grid View控件,然后再将所有更改保存回xml文件(Win Forms应用程序) ).
暂时我忽略"了保存"部分,因为我什至无法使查看/加载"部分正常工作,因此我将尝试在以后继续进行该工作一旦我解决了最初的问题,我就会寻求帮助.

我已经尝试过一些使我抛出异常的事情,但是由于我从未尝试过将数据集绑定到XML,所以我可能正在做一些事情愚蠢或缺少某些东西...

我用以下内容制作了简单的xml文件(命名为:XMLFile1.xml):

Today i was attempting (for the first time) to make a simple application that will allow me to read XML file into Grid View control (via DataSet), and later on save back any changes back to xml file (Win forms application).
For time being i''m "ignoring" the "save" part, as i''m not able even to get the "view/load" part to work proerply, so i will try to get that working later on once i fix the initial problem i''m seeking help for.

I''ve tried something thats throwing me exception but since i''ve never tried binding a dataset to XML i''m probably doing something stupid or missing something...

i''ve made simple xml file (named it: XMLFile1.xml) with the following content:

<br /><?xml version="1.0" encoding="utf-8" ?><br /><item_list><br />  <item><br />    <title>item 1</title><br />    <des>item 1 description</des><br />  </item><br />  <item><br />    <title>item 2</title><br />    <des>item 2 description</des><br />  </item><br /></item_list> <br />



添加:
1.按钮,其默认名称为button1.
2. DataGridView,其默认名称为dataGridView1.
3.在数据网格内部,我创建了2列名称为"title"和名称"的数据集,默认名称为dataSet1. "des".

最终为按钮创建了click事件,其内容如下:



on the form i added:
1. button with default name of button1.
2. DataGridView with default name of dataGridView1.
3. DataSet with default name of dataSet1.

inside the data grid i''ve created 2 columns with names "title" & "des".

finally created click event for button with the following:

<br /> private void button1_Click(object sender, EventArgs e)<br /> {<br />     string xmlPath = @"..\..\XMLFile1.xml";<br /><br />     dataSet1.ReadXml( xmlPath, XmlReadMode.ReadSchema );<br />     dataGridView1.DataMember = "item";<br /> }<br />



现在我遇到的问题是以下行:
dataGridView1.DataMember = "item";
是无法创建字段项目的子列表"的例外.

事实是我不确定此错误是由于我创建了无效的xml文件还是我导致的

如果有人可以帮助我&,我将不胜感激.指出正确的方向.

最好的问候,Idan B.



Now the problem i have is the line:
dataGridView1.DataMember = "item";
is thowing exception of "Child list for field item cannot be created".

The truth is i''m not sure if this error is result of invalid xml file i''ve created, or i''m not using properly the DataMember property.

I would appreciate if anyone could help me & point me in right direction of what i''m doing wrong here.

best regards,
Idan B.

推荐答案

我只是快速浏览了您的帖子,所以我可能离答案还很远,但是看来您走在正确的轨道上.

I just quickly browsed your post, so I may be way off on the answer, but it looks like you are on the right track.

DataSet dsXml = new DataSet();<br />dsXml.ReadXml(fileName);



在DataSet中,您可以有多个DataTable对象,具体取决于XML的格式.我过去所做的是创建一个动态组合框,该组合框获取DataSet中每个表的名称.

您想将DataGridView的DataSource设置为我们读取的DataSet对象(dsXml).然后,当您的组合框更改时,可以将数据成员设置为所选项目的名称.



Within a DataSet you can have multiple DataTable objects depending on the format of the XML. What I''ve done in the past is to create a dynamic combo box that gets the name of every single table in the DataSet.

You would want to set the DataSource of you DataGridView to the DataSet object we read (dsXml). Then when your combo box changes you can set the data member to the name of the selected item.

private void LoadComboBox()<br />{<br />   foreach (DataTable tbl in dsXml.Tables)<br />   {<br />       ComboBox1.Items.Add(tbl.TableName);<br />   }<br />}<br />private void SetSource()<br />{<br />   DataGridView1.DataSource = dsXml;<br />}<br /><br />private void OnComboChanged()<br />{<br />   DataGridView1.DataMember = ComboBox1.Text;<br />}




您还可以将源设置为列表中的特定表.数据集.




You could also set the source to a specific table in the DataSet.

DataGridView1.DataSource = dsXml.Tables[0];


这篇关于WinForms-网格视图绑定到xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆