码迷,mamicode.com
首页 > 其他好文 > 详细

DataGrid通过DataSet保存为xml_测试

时间:2015-08-14 15:14:47      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

做了个小的DataGrid通过DataSet保存为xml_测试,现在有一些问题,xml文件一直为空,DataGrid通过DataSet保存为xml_测试绑定的DataSet不能添加到XML文件中,

请朋友们帮忙看看。

类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_shixun\WinDataSet_xml\WinDataSet_xml\ItemManager.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 { }
        }


    }
}

新建窗体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();
        }
    }
}

新建窗体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);
        }
    }
}

 

DataGrid通过DataSet保存为xml_测试

标签:

原文地址:http://www.cnblogs.com/ammy714926/p/4729943.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!