天天看点

C# DataGridView 保存 xml

创建一个工程,名字自已定义-_-!!! ,放入 DataGridView 、 ComboBox 、两个 Button 。

button1.Text = “保存”       button2.Text = “打开”    comboBox1.DropDownStyle =  ComboBoxStyle.DropDownList;。

首先做一个 xml 文档绑定到 comboBox1.DisplayMember 和 comboBox1.ValueMember ,怎么绑定  请看这里 ,至于为什么要这样绑定,接着看下去就明白了。

要绑定到 comboBox1 的 xml 内容:(示例)

<?xml version="1.0" encoding="GB2312" standalone="true"?>
-<DataName>
 -<Total id="1" name="File name">
 <FileName>文件名1</FileName>
 <FileNameValue>FileName1</FileNameValue>
 </Total>
 -<Total id="2" name="File name">
 <FileName>文件名2</FileName> 
<FileNameValue>FileName2</FileNameValue>
 </Total> 
</DataName>
           

很明显看到 FileName  = 中文名称,而  FileNameValue =英文名称

下面要引入的命名空间,窗体加载,创建绑定 comboBox1 方法。

using System.IO;
using System.Xml;
           
#region - 窗体装载 -
        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Text = "保存";
            button2.Text = "打开";
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox1.Items.Clear(); //清空 comboBox1 集合内容
            GetXmlTable(); //调用 绑定
            this.comboBox1.SelectedIndex = -1; //设置comboBox1 内容不显示
        }
        #endregion
           
/// <summary> - 数据绑定 - 绑定到 ComboBox  
         /// </summary>
        private void GetXmlTable()
        {
            //xml文件所在的路径
           string xmlpath = @"Total.xml"; //注意文件位置 此处表示在工程 \bin\Debug 中
           DataSet xmlds = new DataSet();
           xmlds.ReadXml(xmlpath);

           //ComboBox显示的属性(Text 前台显示) 
           comboBox1.DisplayMember = "FileName";
           //ComboBox实际的属性值(隐藏/后台)
           comboBox1.ValueMember = "FileNamevalue";

           /* comboBox1获取表集合内容,
            * DataSource 要习惯放在后面,
            * 不要放在DisplayMember和ValueMember 的前面
            */ 
          comboBox1.DataSource = xmlds.Tables["Total"];
        }
           

下面是我们绑定 DataGridView 的方法。

#region  - 读取列表 - DatXml(string s)
        /// <summary> 读取列表 </summary>
        /// <param name="s">s 为表名,不要加 .xml 后缀</param>
        /// <returns></returns>
        private void getXml(string s)
        {
            if (File.Exists(s + ".xml"))
            {
                ds1 = new DataSet();
                ds1.ReadXml(s + ".xml");
                dataGridView1.DataSource = ds1.Tables[0];
            }
            else
            {
                DialogResult r = MessageBox.Show(
                    "没有找到" + s + ".xml,\n是否创建" + s + ".xml?",
                    "创建提醒!",
                    MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Question);
                if (r == DialogResult.OK)
                {
                    //string st = comboBox1.SelectedValue.ToString();
                    /* comboBox1.SelectedValue 为comboBox1的 ValueMember 值,
                     * 所以,首先要绑定 ValueMember 值,否则将会返回一个 null 错误 */
                    newTable(s);
                    dataGridView1.DataSource = ds1.Tables[0];
                }
                if (File.Exists(s + ".xml"))
                {
                    DataSet mydat = new DataSet();
                    mydat.ReadXml(s + ".xml");
                    DataTable dt = mydat.Tables[0];
                    dataGridView1.DataSource = dt.DefaultView;
                }
            }
        }
        #endregion
           

打开列表:

private void button2_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex != -1)
            {
                s = comboBox1.SelectedValue.ToString();
                getXml(s);
                comboBox1.Enabled = false;
                button1.ForeColor = Color.Red;
            }
            else { MessageBox.Show("没有选中的列表,请选择一个列表"); }
        }
           

保存:

private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex != -1)
            {
                s = comboBox1.SelectedValue.ToString();
                ds1.WriteXml(s + ".xml");
                comboBox1.Enabled = true;
                button1.ForeColor = Color.Black;
            }
        }
           

创建列表方法:

/// <summary>
        /// 创建新表
        /// </summary>
        /// <param name="s"></param>
        private void newTable(string s)
        {
            //创建XML文件
            DataTable datatable1 = new DataTable();
            datatable1.TableName = s;

            /// <summary> 表示列名 </summary>
            DataColumn column;

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "书籍名称";
            column.AutoIncrement = true;  //列中是否自动递增值
            column.Caption = "书籍名称";
            column.ReadOnly = false;  //是否允许修改行中的值
            column.Unique = false;  //列中的行值是否唯一
            column.DefaultValue = "Null";  //单元格默认值
            datatable1.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "作者";
            column.AutoIncrement = false;
            column.Caption = "作者";
            column.ReadOnly = false;
            column.Unique = false;
            column.DefaultValue = "Null";
            datatable1.Columns.Add(column);    //定多少 列 自己添加

            ds1 = new DataSet();
            ds1.DataSetName = "SysSeting";
            ds1.Tables.Add(datatable1);
        }