//Create a user control name "ucMarketWatch" in the project and put a grid in the control
//Firstly create a class name "Serializer.cs" in the project
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace GridSerialization
{
public class Serializer
{
public Serializer()
{
}
public void SerializeObject(string filename, ObjectToSerialize objectToSerialize)
{
if (filename != null)
{
Stream stream = File.Open(filename, FileMode.Create);
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.Serialize(stream, objectToSerialize);
stream.Close();
}
}
public ObjectToSerialize DeSerializeObject(string filename)
{
ObjectToSerialize objectToSerialize = new ObjectToSerialize();
Stream stream = File.Open(filename, FileMode.Open);
BinaryFormatter bFormatter = new BinaryFormatter();
objectToSerialize = (ObjectToSerialize)bFormatter.Deserialize(stream);
stream.Close();
return objectToSerialize;
}
}
}
Then Create a class with the name "ObjectToSerialize"using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data;
namespace GridSerialization
{
[Serializable()]
public class ObjectToSerialize : ISerializable
{
private List<DataTable> _MarketWatch;
public List<DataTable> MarketWatch
{
get { return this._MarketWatch; }
set { this._MarketWatch = value; }
}
public ObjectToSerialize()
{
}
//Create a property with the name of user control . In this project we use a marketwatch datagridview and pass it as object for the serialization.
public ObjectToSerialize(SerializationInfo info, StreamingContext ctxt)
{
this._MarketWatch = (List<DataTable>)info.GetValue("MarketWatch", typeof(List<DataTable>));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("MarketWatch", this._MarketWatch);
}
}
}
//Now in the user control "ucMarketWatch" put the code given below
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace GridSerialization
{
[Serializable()]
public partial class ucMarketWatch : UserControl, ISerializable
{
DataTable dt = new DataTable();
DataTable dtNevron = new DataTable();
string FileName = null;
string LogFolder = null;
Random rnd = new Random();
public ucMarketWatch()
{
InitializeComponent();
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Age", typeof(string)));
dt.Columns.Add(new DataColumn("EmpNo", typeof(string)));
}
private void ucMarketWatch_Load(object sender, EventArgs e)
{
}
public ucMarketWatch(SerializationInfo info, StreamingContext ctxt)
{
this.dt = (DataTable)info.GetValue("Name", typeof(string));
this.dt = (DataTable)info.GetValue("Age", typeof(string));
this.dt = (DataTable)info.GetValue("EmpNo", typeof(string));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("Name", this.dt);
info.AddValue("Age", this.dt);
info.AddValue("EmpNo", this.dt);
}
List<DataTable> dtList = new List<DataTable>();
public void SerializeMktWatch()
{
dtList.Add(dt);
ObjectToSerialize objectToSerialize = new ObjectToSerialize();
objectToSerialize.MarketWatch = dtList;
Serializer serializer = new Serializer();
SaveFileDialog DialogSave = new SaveFileDialog();
DialogSave.DefaultExt = "tfmw";
DialogSave.AddExtension = true;
DialogSave.RestoreDirectory = true;
DialogSave.Title = "SaveAs";
DialogSave.Filter = "Portfolio (*.tfmw)|*.tfmw|All files (*.*)|*.*";
LogFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + "TradeFortune" + "\\" + "MarketWatch\\";
if (!Directory.Exists(LogFolder))
{
Directory.CreateDirectory(LogFolder, null);
}
DialogSave.InitialDirectory = LogFolder;
if (DialogSave.ShowDialog() == DialogResult.OK)
{
FileName = DialogSave.FileName;
}
DialogSave.Dispose();
DialogSave = null;
serializer.SerializeObject(FileName, objectToSerialize);
}
public List<DataTable> DeSerializeMktWatch()
{
OpenFileDialog openDialog = new OpenFileDialog();
LogFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + "TradeFortune" + "\\" + "MarketWatch\\";
openDialog.InitialDirectory = LogFolder;
if (openDialog.ShowDialog() == DialogResult.OK)
{
FileName = openDialog.FileName;
}
openDialog.Dispose();
openDialog = null;
FileStream Fs = new FileStream(FileName, FileMode.Open);
BinaryFormatter F = new BinaryFormatter();
ObjectToSerialize s1 = (ObjectToSerialize)F.Deserialize(Fs);
List<DataTable> dtnew = s1.MarketWatch;
Fs.Close();
return dtnew;
}
// for add the data at run time in the datagrid view
private void buttonAdd_Click(object sender, EventArgs e)
{
DataRow dr = dt.NewRow();
dr["Name"] = "Ashish";
dr["Age"] = "25";
dr["EmpNo"] = rnd.Next(1,10);
DataRow dr1 = dt.NewRow();
dr1["Name"] = "saurabh";
dr1["Age"] = "22";
dr1["EmpNo"] = rnd.Next(11, 20);
dt.Rows.Add(dr);
dt.Rows.Add(dr1);
dataGridView1.DataSource = dt;
}
}
}
//Now on the form put the user control and write the code for the serealization and Deserialization
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 GridSerialization
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonSerialize_Click(object sender, EventArgs e)
{
ucMarketWatch obj = (ucMarketWatch)this.ucMarketWatch1;
obj.SerializeMktWatch();
}
private void buttonDeserialize_Click_(object sender, EventArgs e)
{
ucMarketWatch obj = (ucMarketWatch)this.ucMarketWatch1;
List<DataTable> dt = obj.DeSerializeMktWatch();
this.ucMarketWatch1.dataGridView1.DataSource = dt[0];
}
}
}
visit http://www.csharpassist.blogspot.com/
ReplyDelete