Advertisement
Guest User

Untitled

a guest
May 5th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.IO;
  9. using System.Runtime.Serialization.Formatters.Binary;
  10. using System.Runtime.Serialization;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13.  
  14. namespace WindowsFormsApplication1
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.  
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         [Serializable]
  25.         public class data
  26.         {
  27.             public string name { get; set; }
  28.             public string surname { get; set; }
  29.             public string city { get; set; }
  30.             public string number { get; set; }
  31.         }
  32.  
  33.         private void toolStripMenuItem1_Click(object sender, EventArgs e)
  34.         {
  35.  
  36.         }
  37.  
  38.         private void saveToolStripMenuItem_Click(object sender, EventArgs e)
  39.         {
  40.             GRID.EndEdit();
  41.             SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  42.             saveFileDialog1.RestoreDirectory = true;
  43.             if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  44.             {
  45.                 BinaryFormatter formatter = new BinaryFormatter();
  46.                 FileStream output = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.Write);
  47.                 int n = GRID.RowCount - 1;
  48.                 data[] Person = new data[n];
  49.  
  50.                 for (int i = 0; i < n; i++)
  51.                 {
  52.                     var tempData = new data();
  53.                     var v = GRID[0, i].Value;
  54.                     tempData.name = v != null ? v.ToString() : string.Empty;
  55.  
  56.                     v = GRID[1, i].Value;
  57.                     tempData.surname = v != null ? v.ToString() : string.Empty;
  58.  
  59.                     v = GRID[2, i].Value;
  60.                     tempData.city = v != null ? v.ToString() : string.Empty;
  61.  
  62.                     v = GRID[3, i].Value;
  63.                     tempData.number = v != null ? v.ToString() : string.Empty;
  64.  
  65.                     Person[i] = tempData;
  66.                 }
  67.                 formatter.Serialize(output, Person);
  68.                 output.Close();
  69.             }
  70.         }
  71.  
  72.         private void openToolStripMenuItem_Click(object sender, EventArgs e)
  73.         {
  74.             openFileDialog1 = new OpenFileDialog();
  75.             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  76.             {
  77.                 BinaryFormatter reader = new BinaryFormatter();
  78.                 FileStream input = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
  79.                 data[] Person = (data[])reader.Deserialize(input);
  80.                 GRID.Rows.Clear();
  81.                 var bindingList = new BindingList<data>();
  82.                 for (int i = 0; i < Person.Length; i++)
  83.                 {
  84.                     var p = Person[i];
  85.                     bindingList.Add(new data() { name = p.name, surname = p.surname, city = p.city, number = p.number });
  86.  
  87.                 }
  88.                 GRID.DataSource = bindingList;
  89.                 input.Close();
  90.             }
  91.         }
  92.  
  93.         private void closeToolStripMenuItem_Click(object sender, EventArgs e)
  94.         {
  95.             Close();
  96.         }
  97.  
  98.         private void Form1_Load(object sender, EventArgs e)
  99.         {
  100.  
  101.         }
  102.  
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement