Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Runtime.Serialization;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- [Serializable]
- public class data
- {
- public string name { get; set; }
- public string surname { get; set; }
- public string city { get; set; }
- public string number { get; set; }
- }
- private void toolStripMenuItem1_Click(object sender, EventArgs e)
- {
- }
- private void saveToolStripMenuItem_Click(object sender, EventArgs e)
- {
- GRID.EndEdit();
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.RestoreDirectory = true;
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- BinaryFormatter formatter = new BinaryFormatter();
- FileStream output = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.Write);
- int n = GRID.RowCount - 1;
- data[] Person = new data[n];
- for (int i = 0; i < n; i++)
- {
- var tempData = new data();
- var v = GRID[0, i].Value;
- tempData.name = v != null ? v.ToString() : string.Empty;
- v = GRID[1, i].Value;
- tempData.surname = v != null ? v.ToString() : string.Empty;
- v = GRID[2, i].Value;
- tempData.city = v != null ? v.ToString() : string.Empty;
- v = GRID[3, i].Value;
- tempData.number = v != null ? v.ToString() : string.Empty;
- Person[i] = tempData;
- }
- formatter.Serialize(output, Person);
- output.Close();
- }
- }
- private void openToolStripMenuItem_Click(object sender, EventArgs e)
- {
- openFileDialog1 = new OpenFileDialog();
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- BinaryFormatter reader = new BinaryFormatter();
- FileStream input = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
- data[] Person = (data[])reader.Deserialize(input);
- GRID.Rows.Clear();
- var bindingList = new BindingList<data>();
- for (int i = 0; i < Person.Length; i++)
- {
- var p = Person[i];
- bindingList.Add(new data() { name = p.name, surname = p.surname, city = p.city, number = p.number });
- }
- GRID.DataSource = bindingList;
- input.Close();
- }
- }
- private void closeToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Close();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement