Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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, Blue, Green, etc, if the user selects, for example, Red, the selected cell sets its BackColor to Red, and the user is also able to write a value in that cell. Well, my problem is that, I cannot find a way to save all the content and style, so if the user re-opens the for, the dataGridView wil have its last settings (including the BackColor and ForeColor of the cells).

I tried this to save the content, it gave me error, and I don't know how to open it.

C#
private void button4_Click(object sender, EventArgs e)
   {
       SaveFileDialog svd = new SaveFileDialog();
       svd.Filter = "XML Files (*.xml)|*.xml";

       if(svd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
       {
           DataTable dt = ((DataView)this.dataGridView1.DataSource).Table;
           dt.WriteXml(svd.FileName);
       }
   }

If there is a better way to save the content and style, it is appreciated. Thanks in Advance
Posted
Comments
ZurdoDev 3-Feb-15 14:20pm    
What error did you get?
ChrisCreateBoss 3-Feb-15 14:28pm    
reference to object not set as an object's instance
ZurdoDev 3-Feb-15 14:38pm    
1. That error has nothing to do with your end goal but whatever line that error happens on means something is null.
2. I believe the DataTable.WriteXml method only writes data, not styling so I don't think this approach will work anyway.
ChrisCreateBoss 3-Feb-15 14:58pm    
Yes, I know it doesn't save any information about styles, but is there another way to save styling?? 'Cause I need my application to remember that cells' backcolor.
ZurdoDev 3-Feb-15 15:01pm    
Nothing built-in that I am aware of. However, this should help you: https://social.msdn.microsoft.com/Forums/windows/en-US/eeddaab5-5b39-42e7-93cf-3208aaa13b52/datagridview-saving-data-and-style-ie-column-widths-and-background-colour-etc?forum=winforms

I would create two tables:
grid_styles_header (id, grid_control_id, user_id)
grid_styles_cells (id, header_id, row_unique_identifier, column_name, color)

WHERE
header_id from cells references id from header
row_unique_identifier is your primary key for the query so you can know which row to update uniquely instead of using indexes.
column_name - datagrid column name or database column name so you know which column to affect regardless of column reordering some users might try.

I'm not aware of any built-in solution, but there may be third party controls that allow this.

If this helps please take time to accept the solution. Thank you.


Note: same saving logic goes for XML except you have node
<grid id="" user_id="">
<cell row_id="" column_name="" color="" />
<cell row_id="" column_name="" color="" />
<cell row_id="" column_name="" color="" />
</grid>


Good luck
 
Share this answer
 
v2
If you want to write DataTable as XML you must give name to DataTable. e.g. dt.TableName = "XYZ" and dt.WriteXML(...).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900