Save and load DataGridView Content and Style in C#

前端 未结 1 1723
野的像风
野的像风 2021-01-24 12:04

I have a DataGridView with many columns and rows, the user is able to right click in a cell and select an option from a ContextMenuStrip. The options are in Colors like Red, Blu

1条回答
  •  醉梦人生
    2021-01-24 12:50

    If persistent cell formatting is all you want, the following type of object serialization will work for you.

    Create a class to serialize your desired properties:

    public class SavedSettings
    {
        [XmlIgnore]
        public Color ForeColor { get; set; }
    
        [XmlElement("ForeColor")]
        public int ForeColorARGB
        {
          get { return this.ForeColor.ToArgb(); }
          set { this.ForeColor = Color.FromArgb(value); }
        }
    
        [XmlIgnore]
        public Color BackColor { get; set; }
    
        [XmlElement("BackColor")]
        public int BackColorARGB
        {
          get { return this.BackColor.ToArgb(); }
          set { this.BackColor = Color.FromArgb(value); }
        }
    
        public object Value { get; set; }
    }
    

    Within your main class, load any saved settings from xml:

    public List Settings { get; set; }
    
    private void ReadXML()
    {
      System.Xml.Serialization.XmlSerializer reader =
          new System.Xml.Serialization.XmlSerializer(typeof(List));
    
      if (File.Exists(@"SavedSettings.xml"))
      {
        System.IO.StreamReader file = new System.IO.StreamReader(
          @"SavedSettings.xml");
        this.Settings = (List)reader.Deserialize(file);
        file.Close();
      }
    }
    
    private void LoadDGV()
    {
      this.ReadXML();
    
      if (this.Settings != null)
      {
        // This assumes your dgv has added columns already.
        int rows = this.Settings.Count / this.dataGridView1.Columns.Count;
        int cols = this.dataGridView1.Columns.Count;
    
        this.dataGridView1.Rows.AddCopies(0, rows);
    
        for (int i = 0; i < this.Settings.Count; i++)
        {
          int row = i / cols;
          int col = i % cols;
          this.dataGridView1[col, row].Style.BackColor = this.Settings[i].BackColor;
          this.dataGridView1[col, row].Style.ForeColor = this.Settings[i].ForeColor;
          this.dataGridView1[col, row].Value = this.Settings[i].Value;
        }
      }
    }
    

    Then, when you're ready to save, reload your cell settings into the object array and serialize it:

    private void SaveSettings()
    {
      this.Settings = new List();
    
      foreach (DataGridViewRow row in this.dataGridView1.Rows)
      {
        if (!row.IsNewRow)
        {
          foreach (DataGridViewCell cell in row.Cells)
          {
            SavedSettings setting = new SavedSettings();
            setting.BackColor = cell.Style.BackColor.ToArgb() == 0 ? Color.White : cell.Style.BackColor;
            setting.ForeColor = cell.Style.ForeColor.ToArgb() == 0 ? Color.Black :  cell.Style.ForeColor; ;
            setting.Value = cell.Value;
    
            this.Settings.Add(setting);
          }
        }
      }
    
      this.WriteXML();
    }
    
    private void WriteXML()
    {
      System.Xml.Serialization.XmlSerializer writer =
          new System.Xml.Serialization.XmlSerializer(typeof(List));
    
      System.IO.StreamWriter file = new System.IO.StreamWriter(@"SavedSettings.xml");
      writer.Serialize(file, this.Settings);
      file.Close();
    }
    

    Note This will allow certain properties to persist. Surely there are better methods and I'd love to learn them as time allows, but this example can do the job. As for the additional Excel requirement, I have not tried that to date. I would supplement your question to reflect that requirement to attract Excel-oriented answers.

    0 讨论(0)
提交回复
热议问题