简体   繁体   中英

write datagridview data in to xml file

I am new to c#, this may be very easy question.

In my application there is a Form in which I have datagridview dgvBill and a Button btnClick . I declared columns to it by clicking the extender and selecting add columns . After executing the form, I written some text to each column in dgvBill . When I click on btnClick it is creating an xml file but with no data in it. (why the data is not being added ?)

The created XML file looks like this:

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

I have tried the below code:

btnClick Event

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

lblCustId is a label in the Form

Optional : can I make each cell of datagridview into ComboBox?

Thanks in advance.

use dataGridView1.EndEdit(); and then create the XML

I think you are writing XML before assigning the dataset to dgvBill. Try putting after.

First create binding source and load data from binding source to datatable and create a new dataset and add datatable to dataset and then write to XML file from dataset.

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"); 

My problem solved when I created a new empty xml file (which has only created column names) and read the xml file in a dataset object (on form load event). I used the same dataset object to write on different xml file (on button click event). I am sure that this is not the correct way but it worked. Please post here if you have any other good suggestions.

COuld you check that you have set the datapropertyname for your columns, Here is my code. This works fine 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

namespace WindowsFormsApplication1 { partial class Form32 { /// /// Required designer variable. /// 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;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM