【问题标题】:write datagridview data in to xml file将datagridview数据写入xml文件
【发布时间】:2013-05-21 13:18:03
【问题描述】:

我是 C# 新手,这可能是一个非常简单的问题。

在我的应用程序中有一个Form,其中我有datagridview dgvBill 和一个按钮btnClick。我通过单击扩展器并选择add columns 为其声明了列。执行表单后,我在dgvBill 的每一列中写入了一些文本。当我单击 btnClick 时,它正在创建一个 xml 文件,但其中没有数据。 (为什么没有添加数据?)

创建的 XML 文件如下所示:

<?xml version="1.0" standalone="yes"?>
<NewDataSet />

我试过下面的代码:

btnClick 事件

DataSet ds = new DataSet();
ds.WriteXml(@"..\..\Customer_Info\" + lblCustId.Text + ".xml");
dgvBill.DataSource = ds;  /* also tried ds.Tables[0] */

lblCustIdForm 中的标签

可选:我可以将datagridview的每个单元格都做成ComboBox吗?

提前致谢。

【问题讨论】:

    标签: c# xml datagridview


    【解决方案1】:

    使用dataGridView1.EndEdit();,然后创建XML

    【讨论】:

    【解决方案2】:

    我认为您在将数据集分配给 dgvBill 之前正在编写 XML。尝试放在后面。

    【讨论】:

      【解决方案3】:

      首先创建绑定源并将数据从绑定源加载到数据表,然后创建一个新数据集并将数据表添加到数据集,然后从数据集写入 XML 文件。

      BindingSource bs = (BindingSource )MyGridView.DataSource;
      DataTable dt= (DataTable ) bs.DataSource;
      DataSet ds = new DataSet();
      ds.Tables.Add(dt);
      ds.Tables[0].WriteXml("E:\\test2.xml"); 
      

      【讨论】:

      • 它显示错误 - “对象引用未设置为对象的实例。” 错误点: DataTable dt = (DataTable)bs.DataSource; 。顺便说一句,感谢您的回复。
      • 我认为你的 dataGrid 是空的。检查一下。
      • 是的,加载时它是空的。执行我的应用程序后,我正在其中写一些文本。这不会添加到 XML 文件中。请帮忙。
      • 你把那行放在你的代码中 dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
      • 是的,我在 buttonclick 事件中创建了这个数据源,而表单加载事件中的 datagridview 为空。 here is my code.
      【解决方案4】:

      当我创建一个新的空 xml 文件(它只创建了列名)时,我的问题解决了 并读取数据集对象中的 xml 文件(在表单加载事件中)。我使用相同的数据集对象写入不同的 xml 文件(在按钮单击事件上)。我确信这不是正确的方法,但它确实有效。如果您有任何其他好的建议,请在此处发布。

      【讨论】:

        【解决方案5】:

        您能否检查一下您是否为列设置了 datapropertyname, 这是我的代码。这工作正常 form3.cs

        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;
        using System.IO;
         namespace WindowsFormsApplication1
         {
          public partial class Form32 : Form
            {
            public Form32()
            {
                InitializeComponent();
            }
        
            private void button1_Click(object sender, EventArgs e)
            {
                DataSet ds = new DataSet();
                DataTable dt = new DataTable("row");
                dt.Columns.Add("Id", typeof(int));
                dt.Columns.Add("UserName", typeof(string));
                dt.Rows.Add(1, "Tamer");
                dt.Rows.Add(2, "Foo");
                ds.Tables.Add(dt);
                dataGridView1.DataSource = ds;
                dataGridView1.DataMember = "row";
            }
        
            private void Form3_Load(object sender, EventArgs e)
            {
                string fileName = @"C:\users\tamer\desktop\data.xml";
                if (File.Exists(fileName))
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml(fileName);
                    dataGridView1.DataSource = ds;
                    dataGridView1.DataMember = "row";
                }
            }
        
            private void Form3_FormClosed(object sender, FormClosedEventArgs e)
            {
                string fileName = @"C:\users\tamer\desktop\data.xml";
                DataSet dataSet = (DataSet)dataGridView1.DataSource;
                dataSet.WriteXml(fileName);
        
            }
        }
        

        }

        form3.designer.cs

        命名空间 WindowsFormsApplication1 { 部分类 Form32 { /// /// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null;

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
        
            #region Windows Form Designer generated code
        
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.dataGridView1 = new System.Windows.Forms.DataGridView();
                this.button1 = new System.Windows.Forms.Button();
                this.Id = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.UserName = new System.Windows.Forms.DataGridViewTextBoxColumn();
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
                this.SuspendLayout();
                // 
                // dataGridView1
                // 
                this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                this.Id,
                this.UserName});
                this.dataGridView1.Location = new System.Drawing.Point(0, 0);
                this.dataGridView1.Name = "dataGridView1";
                this.dataGridView1.Size = new System.Drawing.Size(582, 337);
                this.dataGridView1.TabIndex = 0;
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(246, 377);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 1;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // Id
                // 
                this.Id.DataPropertyName = "Id";
                this.Id.HeaderText = "Id";
                this.Id.Name = "Id";
                // 
                // UserName
                // 
                this.UserName.DataPropertyName = "UserName";
                this.UserName.HeaderText = "UserName";
                this.UserName.Name = "UserName";
                // 
                // Form32
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(632, 431);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.dataGridView1);
                this.Name = "Form32";
                this.Text = "Form3";
                this.Load += new System.EventHandler(this.Form3_Load);
                this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form3_FormClosed);
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
                this.ResumeLayout(false);
        
            }
        
            #endregion
        
            private System.Windows.Forms.DataGridView dataGridView1;
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.DataGridViewTextBoxColumn Id;
            private System.Windows.Forms.DataGridViewTextBoxColumn UserName;
        }
        

        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多