做了个小的DataGrid通过DataSet保存为xml_测试,DataGrid通过DataSet保存为xml_测试,通过dataSet.writeXML()和dataSet.readXML()方法完成了写入和自动读取,在一些大型项目中,xml作为一些不经常修改的配置文件的作用就显得很重要!也可以试验DataSet从数据库获取ROW,请见我的上一篇文章~
本项目为了结构逻辑更加清晰,使用了分层结构,包括一个class文件和form文件~
类ItemList代码<主要是一些ITEM属性和dataSet的writexml方法>:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data;//使用dataset,对于form则自动引用此命名空间,类文件需手动添加 namespace WinDataSet_xml { public class ItemList //class若没有publicl修饰符,则会出现错误 //1 Inconsistent accessibility: parameter type 'WinDataSet_xml.ItemList' is less accessible than //method 'WinDataSet_xml.ItemManager.Show(WinDataSet_xml.ItemList)' E:winform_shixunWinDataSet_xmlWinDataSet_xmlItemManager.cs 22 21 WinDataSet_xml { private Array m_ItemId; private Array m_ItemName; private Array m_ItemInfo; private string m_ItemListFileName = "ItemList.xml"; private string m_XMLPath = AppDomain.CurrentDomain.BaseDirectory + "ItemList.xml"; private DataSet m_Ds = new DataSet(); #region get,set Property public Array ItemID { get { return m_ItemId;} set {this.m_ItemId=value;} } public Array ItemName{get {return m_ItemName;} set {this.m_ItemName=value;} } public Array ItemInfo { get { return m_ItemInfo; } set { this.m_ItemInfo = value; } } public DataSet ItemDS { get { return m_Ds; } set { this.m_Ds = value; } } public string ItemListFileName { get { return m_ItemListFileName; } set { this.m_ItemListFileName = value; if (value != null && !string.IsNullOrEmpty(value)) { m_XMLPath = AppDomain.CurrentDomain.BaseDirectory + "\" + this.m_ItemListFileName; } } } public DataSet ItemDataSet { get { DataSet ds = new DataSet(); ds.Tables.Add(new DataTable("Item属性表")); ds.Tables[0].Columns.Add(new DataColumn("Item序列号")); ds.Tables[0].Columns.Add(new DataColumn("Item姓名")); ds.Tables[0].Columns.Add(new DataColumn("Item信息")); return ds;//get属性就是要return,否则出错! } } #endregion public void SaveXML() { try { this.m_Ds.WriteXml(this.m_XMLPath); } catch(Exception ex) { throw(ex); } } public void LoadingXML() { try { this.m_Ds.ReadXml(this.m_XMLPath); } catch (Exception ex) { throw (ex); } } } }
新建窗体ItemManager.cs代码<此窗体中含有dataGride控件及button控件>:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WinDataSet_xml { public partial class ItemManager : Form { public ItemManager() { InitializeComponent(); } //同项目下的类可以互相引用,不同项目下需要using private ItemList _Items; public void Show(ItemList items) { this._Items = items; ItemList11.DataSource = _Items.ItemDS.Tables[0];//ItemList1是网格Name,设置网格所显示的数据的数据源 //ItemList11.DataSource = _Items.ItemDataSet.Tables[0].DefaultView; this.Show(); } private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < this.ItemList11.RowCount;i++ ) { DataRow dr = null; dr = this._Items.ItemDS.Tables[0].NewRow(); dr[0]=ItemList11.Rows[i].Cells[0].Value; dr[1]=ItemList11.Rows[i].Cells[1].Value; dr[2]=ItemList11.Rows[i].Cells[2].Value; //this._Items.ItemDS.Tables[0].Rows.Add(dr);这个是一样的,不必写!写了反而不对 //this.ItemList11.DataSource = this._Items.ItemDS.Tables[0]; } this._Items.SaveXML(); } private void button2_Click(object sender, EventArgs e) { this._Items.LoadingXML(); } } }
新建窗体Form_Base.cs<MdiParent>代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WinDataSet_xml { public partial class Form_Base : Form { public Form_Base() { InitializeComponent(); } private void Button_Management_Click(object sender, EventArgs e) { ItemManager _ItemManager = new ItemManager(); _ItemManager.MdiParent = this; _ItemManager.Dock = DockStyle.Fill; ItemList _ItemListDG = new ItemList(); DataSet _ds = _ItemListDG.ItemDataSet; _ItemListDG.ItemDS = _ds; // _ItemListDG.ItemDataSet = _ds; 只有get方法不能赋值 _ItemManager.Show(_ItemListDG); } } }
测试效果图: