Quantcast
Channel: CSharp Forum Latest Questions
Viewing all articles
Browse latest Browse all 32058

preserving datagridview cell format when saved as table

$
0
0

Hi,


I have a DataGridView whos DataSource is a DataTable.

 I save the DataSet to file by dataSet.WriteXml(file,XmlWriteMode.WriteSchema);

  I read back the DataSet from the file by dataSet.ReadXml(file,XmlReadMode.ReadSchema);

 I can modify the data in the DataGridView and it will automatically update the DataSource table, be saved to the file and retrived.

 All works fine.

 I then make changes to the DataGridView cell format (example background color), but that formatting is not saved in the file and thus it is not read back and dose not show on the DataGridView. (I guess a data table does not have cell formatting?)

 Does anyone know of a way to preserve the DataGridView cell format (via the source table?) or any other idea?

  Simplified working code:

       public Form1()
         {
             InitializeComponent();
             makeTable();
         }
 
 
         DataTable dataTable = new DataTable();
         DataSet dataSet = new DataSet();
         string file = "c:\\temp\\test.xml";

   public void makeTable()
         {
             //
             // Create a DataTable with four columns.
             //
             dataTable.Columns.Add("#", typeof(int));
             dataTable.Columns.Add("weekday", typeof(int));
             dataTable.Columns.Add("weekend", typeof(int));
             //
             // Add 24 Int DataRows. First row = index.
             //

             for (int i = 0; i < 24; i++)
             {

                 dataTable.Rows.Add(i);

             }

             dataSet.Tables.Add(dataTable);  //Include in a dataset

             dataGrid.DataSource = dataTable; //Attach the table to the grid.

  //Save the DataSet to a file

     public void saveToFile()
         {

             dataSet.WriteXml(file, XmlWriteMode.WriteSchema);
         }

  \\ Read back the DataSet from file
  public void readFromFile()
         {

             dataSet.Clear();
             dataSet.ReadXml(file, XmlReadMode.ReadSchema);
         }

  }


   


Viewing all articles
Browse latest Browse all 32058

Trending Articles