C#, XML to datagridvew for data load, edits and then save back (serialize and deserialize)

Based on earlier discussion, the datagridview would be better solution than listbox since it display member and associated data.

I have setup the datagridview and bind it which works fine.

The question is
(1) Would I benefit using Dataset to manage the data, if so why?, I do not need to sort or copy but need to make data member name visable as in datagridview.
(2) Once the data is modified within datagridview, I then click a button to save it (serialize), what code needed to do that?
(3)  A demo code would be very useful.
Richard PayneChief Technology EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
A dataset is probably overkill for your needs right now.  In reality, datasets are more beneficial when dealing with multiple tables and relationships between them.  With your current data model, I wouldn't make it more complex than needed.  Perhaps something like:

Form1.cs -
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace EE_Q28741920
{
	public partial class Form1 : Form
	{
		List<ConfigurationX> configurations;

		public Form1()
		{
			InitializeComponent();

			if (File.Exists("Configurations.xml"))
			{
				configurations = DeserializeFromFile<List<ConfigurationX>>("Configurations.xml");
			}
			else
			{
				if (configurations == null)
				{
					configurations = new List<ConfigurationX>();
					for (int i = 0; i < 10; i++)
						configurations.Add(new ConfigurationX() { CompanyID = (uint)i, CompanyString = string.Format("ZMDI{0}", i) });
				}
				SerializeToFile<List<ConfigurationX>>("Configurations.xml");
			}
		}

		private void OnLoad(object sender, EventArgs e)
		{
			bindingSource1.DataSource = configurations;
			dataGridView1.DataSource = bindingSource1;
		}

		T DeserializeFromFile<T>(string fileName) where T : class
		{
			if (typeof(T).IsSerializable)
			{
				using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
				{
					XmlSerializer serializer = new XmlSerializer(typeof(T));
					return (T)serializer.Deserialize(stream);
				}
			}
			return default(T);
		}

		void SerializeToFile<T>(string fileName) where T : class
		{
			if (typeof(T).IsSerializable)
			{
				if (File.Exists("Configurations.xml"))
					File.Delete("Configurations.xml");

				using (var stream = new FileStream(fileName, FileMode.CreateNew, FileAccess.ReadWrite))
				{
					XmlSerializer serializer = new XmlSerializer(typeof(T));
					serializer.Serialize(stream, configurations);
				}
			}
		}

		private void OnClosing(object sender, FormClosingEventArgs e)
		{
			SerializeToFile<List<ConfigurationX>>("Configurations.xml");
		}
	}

	[Serializable]
	public class ConfigurationX
	{
		public uint CompanyID { get; set; }
		public string CompanyString { get; set; }
		public uint Test1 { get; set; }
		public uint Test2 { get; set; }
		public uint Test3 { get; set; }

		public ConfigurationX()
		{
			// Leave blank, do not install default data here.
		}

		public static ConfigurationX Default()
		{
			return new ConfigurationX() { CompanyID = 10, CompanyString = "ZMDI", Test1 = 9678, Test2 = 34453, Test3 = 12981 };
		}
	}

	class SortableBindingList<T> : BindingList<T>
	{
		private bool IsSorted;
		private ListSortDirection SortDirection;
		private PropertyDescriptor SortProperty;

		protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
		{
			var items = Items as List<T>;
			if (items == null)
				IsSorted = false;
			else
			{
				var comparer = new PropertyCompare<T>(property.Name, direction);
				items.Sort(comparer);
				IsSorted = true;
				SortDirection = direction;
				SortProperty = property;
			}
			base.ApplySortCore(property, direction);
		}

		protected override bool IsSortedCore { get { return IsSorted; } }
		protected override void RemoveSortCore() { IsSorted = false; }
		protected override ListSortDirection SortDirectionCore { get { return SortDirection; } }
		protected override PropertyDescriptor SortPropertyCore { get { return SortProperty; } }
		protected override bool SupportsSortingCore { get { return true; } }

		public SortableBindingList() { ;}
		public SortableBindingList(ICollection<T> source) : base(source as List<T>) { ;}
	}

	class PropertyCompare<T> : IComparer<T>
	{
		private PropertyInfo PropertyInfo;
		private ListSortDirection SortDirection;

		protected internal PropertyCompare(string property, ListSortDirection direction)
		{
			PropertyInfo = typeof(T).GetProperty(property);
			SortDirection = direction;
		}

		public int Compare(T x, T y)
		{
			return (SortDirection.Equals(ListSortDirection.Ascending)) ?
				Comparer.Default.Compare(PropertyInfo.GetValue(x, null), PropertyInfo.GetValue(y, null)) :
				Comparer.Default.Compare(PropertyInfo.GetValue(y, null), PropertyInfo.GetValue(x, null));
		}
	}
}

Open in new window

Form1.Designer.cs -
namespace EE_Q28741920
{
	partial class Form1
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		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.components = new System.ComponentModel.Container();
			this.dataGridView1 = new System.Windows.Forms.DataGridView();
			this.bindingSource1 = new System.Windows.Forms.BindingSource(this.components);
			((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).BeginInit();
			this.SuspendLayout();
			// 
			// dataGridView1
			// 
			this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.dataGridView1.Location = new System.Drawing.Point(0, 0);
			this.dataGridView1.Name = "dataGridView1";
			this.dataGridView1.RowHeadersVisible = false;
			this.dataGridView1.Size = new System.Drawing.Size(530, 295);
			this.dataGridView1.TabIndex = 0;
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(530, 295);
			this.Controls.Add(this.dataGridView1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.OnClosing);
			this.Load += new System.EventHandler(this.OnLoad);
			((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).EndInit();
			this.ResumeLayout(false);

		}

		#endregion

		private System.Windows.Forms.DataGridView dataGridView1;
		private System.Windows.Forms.BindingSource bindingSource1;
	}
}

Open in new window

Which produces the following output -Capture.JPGAnd allows for us to edit the data directly in the grid.After we close the form the changes are automatically saved to the Xml file:
<?xml version="1.0"?>
<ArrayOfConfigurationX xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ConfigurationX>
    <CompanyID>0</CompanyID>
    <CompanyString>ZMDI0</CompanyString>
    <Test1>12</Test1>
    <Test2>0</Test2>
    <Test3>0</Test3>
  </ConfigurationX>
  <ConfigurationX>
    <CompanyID>1</CompanyID>
    <CompanyString>ZMDI1</CompanyString>
    <Test1>0</Test1>
    <Test2>12</Test2>
    <Test3>0</Test3>
  </ConfigurationX>
  <ConfigurationX>
    <CompanyID>2</CompanyID>
    <CompanyString>ZMDI2</CompanyString>
    <Test1>0</Test1>
    <Test2>0</Test2>
    <Test3>12</Test3>
  </ConfigurationX>
  <ConfigurationX>
    <CompanyID>3</CompanyID>
    <CompanyString>ZMDI3</CompanyString>
    <Test1>12</Test1>
    <Test2>0</Test2>
    <Test3>0</Test3>
  </ConfigurationX>
  <ConfigurationX>
    <CompanyID>4</CompanyID>
    <CompanyString>ZMDI4</CompanyString>
    <Test1>0</Test1>
    <Test2>12</Test2>
    <Test3>0</Test3>
  </ConfigurationX>
  <ConfigurationX>
    <CompanyID>5</CompanyID>
    <CompanyString>ZMDI5</CompanyString>
    <Test1>0</Test1>
    <Test2>0</Test2>
    <Test3>12</Test3>
  </ConfigurationX>
  <ConfigurationX>
    <CompanyID>6</CompanyID>
    <CompanyString>ZMDI6</CompanyString>
    <Test1>12</Test1>
    <Test2>0</Test2>
    <Test3>0</Test3>
  </ConfigurationX>
  <ConfigurationX>
    <CompanyID>7</CompanyID>
    <CompanyString>ZMDI7</CompanyString>
    <Test1>0</Test1>
    <Test2>12</Test2>
    <Test3>0</Test3>
  </ConfigurationX>
  <ConfigurationX>
    <CompanyID>8</CompanyID>
    <CompanyString>ZMDI8</CompanyString>
    <Test1>0</Test1>
    <Test2>0</Test2>
    <Test3>12</Test3>
  </ConfigurationX>
  <ConfigurationX>
    <CompanyID>9</CompanyID>
    <CompanyString>ZMDI9</CompanyString>
    <Test1>12</Test1>
    <Test2>0</Test2>
    <Test3>0</Test3>
  </ConfigurationX>
</ArrayOfConfigurationX>

Open in new window

-saige-
0
it_saigeDeveloperCommented:
Just bear in mind that the above is super simplistic.  It does not catch exceptions that could be thrown and does not do things like prevent people from editing any of the data.  It also does not ask if you want to save the changes, the program always deletes and recreates the file based upon the configurations list at the time of closing the form.

-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richard PayneChief Technology EngineerAuthor Commented:
Well done....I take it from there with much thank!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.