Member Avatar for kardo

Hi,

I'm about to develope a little application in C# where I need to save some data in an XML file. I'm viewing data from XML file in a DataGridView, but I would like to make changes and save it in the same XML file. I can't get it to save when i make changes or adding new data in the DataGridView.
Here is some code:

public Form1()
        {
            InitializeComponent();
            dataSet1.ReadXml("C:\\tmp\\test.xml");
            this.bindingSource1.DataSource = dataSet1.Tables["Row"];
            dataGridView1.DataSource = bindingSource1;
        }

I'm using Save button from a bindingNavigator to save:

private void saveToolStripButton_Click(object sender, EventArgs e)
        {

            bindingSource1 = (BindingSource)dataGridView1.DataSource;
            DataTable dt = new DataTable();
            dt = (DataTable)bindingSource1.DataSource;
            dt.WriteXml("C:\\tmp\\test.xml");
}

I would be glad if you could help.
Thanks in advance.

The problem is that you do changes in dgv. So if you want to write all the data from dgv to xml, its best to use DataTable object. Create in on form load, and when you want to save the data into xml, pass data from dgv to dataTable, and from there to xml file:

private void Form1_Load(object sender, EventArgs e)
        {
            DataGridView dataGridView1 = new DataGridView();
            DataTable dt = new DataTable("testtable");
            dt.Columns.Add("id", typeof(int));
            dt.Columns.Add("name", typeof(string));
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
                dt.Rows.Add(dataGridView1[0, i].Value.ToString(), dataGridView1[1, i].Value.ToString()); 
            dt.AcceptChanges();
            this.dataGridView1.DataSource = dt.DefaultView;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = ((DataView)this.dataGridView1.DataSource).Table;
            dt.WriteXml(@"C:\test\text.xml");
        }
commented: 1 +0

I did some example code for you, which uses a bindingSource and import, export of data to/from xml.
What you have to do: Put tow buttons (addRow and SaveToXml) and a dataGridView Controls on form, and test this code:

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 Jan30Exercise2
{
    public partial class Form1 : Form
    {
        BindingList<MyData> list;
        public Form1()
        {
            InitializeComponent();
            CreatingDGV();
            GetDataFromXML();
            CreatingDataTable();
        }

        public void GetDataFromXML()
        {
            DataTable dt = CreatingDataTable();
            dt.ReadXml(@"C:\1\test9.xml");
            foreach (DataRow dr in dt.Rows)
                list.Add(new MyData { Id = Convert.ToInt32(dr[0]), Name = dr[1].ToString() });
        }

        private void CreatingDGV()
        {
            list = new BindingList<MyData>();
            dataGridView1.DataSource = list;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);

            //string[] names = new string[] { "Person 1", "Person 2", "Person 3", "Person 4" };
            //for (int i = 0; i < names.Length; i++)
              //  list.Add(new MyData { Id = (i + 1), Name = names[i] });
        }

        private DataTable CreatingDataTable()
        {
            DataTable dt = new DataTable("Users");
            dt.Columns.Add("id", typeof(int));
            dt.Columns.Add("userName", typeof(string));
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
                dt.Rows.Add(dataGridView1[0, i].Value.ToString(), dataGridView1[1, i].Value.ToString());
            dt.AcceptChanges();
            return dt;
        }

        private void buttonToXml_Click(object sender, EventArgs e)
        {
            DataTable dt = CreatingDataTable();
            dt.WriteXml(@"C:\1\test9.xml");
        }

        internal class MyData
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

        private void buttonNewRow_Click(object sender, EventArgs e)
        {
            list.Add(new MyData { Id = list.Count + 1, Name = "" });
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            list[e.RowIndex].Name = dataGridView1[1, e.RowIndex].Value.ToString();
        }       
    }
}

Hope it helps a bit,
Mitja

Member Avatar for kardo

Thanks for quick reply, you gave me a tip to how I should do it:

private void Form1_Load(object sender, EventArgs e)
        {
            DataGridView dataGridView1 = new DataGridView();
            DataTable dt = new DataTable("Row");
            dt.ReadXmlSchema("C:\\tmp\\test.xml");
            dt.ReadXml("C:\\tmp\\test.xml");
            //dt.Columns.Add("id", typeof(int));
            //dt.Columns.Add("name", typeof(string));
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
                dt.Rows.Add(dataGridView1[0, i].Value.ToString(), dataGridView1[1, i].Value.ToString());
            dt.AcceptChanges();
            this.dataGridView1.DataSource = dt.DefaultView;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.