Saving DataGridView setting in xml and restore it!

This article shows how to save the user manipulation on the grid and restore those settings when the user runs the program.

When the user changes the DataGridView column order or changes the each column size or so on, we need to save these settings so we can restore the user DataGridView settings for the user to have the datagrid changes. To do this I'm going to create the XML file so I could store the settings.
I create 2 functions named ReadDataGridViewSetting() and WriteGrideViewSetting(), each one of them gets 2 parameters.
  1. writing function: I get 2 paramters, the first paramter is the datagrid that we want to save the settings for and the second is the name of the file that we want to save as XML like a.xml or setting.xml.

  2. reading: The Reading function takes 2 parameters, the first is the DataGridView that we want to restore its settings for and the second param is the name of the file that we saved as XML so then we read the paramter from the file then set each DataGridView column setting.

Then I initialize the grid and show it on the form.

I explained the rest of the function operations on the code.

Thank you for your time.
  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. //these two namespace are required  
  9. using System.Xml;  
  10. using System.IO;  
  11. //......  
  12. using System.Threading.Tasks;  
  13. using System.Windows.Forms;  
  14. namespace GridSetting  
  15. {  
  16. public partial class Form1 : Form  
  17. {  
  18. public Form1()  
  19. {  
  20. InitializeComponent();  
  21. }  
  22. //function which read the seting from the xml file  
  23. public void ReadDataGridViewSetting(DataGridView dgv, string FileName)  
  24. {  
  25.   
  26. //declare the xmldocument object  
  27. XmlDocument xmldoc = new XmlDocument();  
  28. //and also xmllistnode  
  29. XmlNodeList xmlnode;  
  30. //declare the filestream for reading and accessing the xml file  
  31. FileStream fs = new FileStream("f:\\XML\\" + FileName + ".xml", FileMode.Open,FileAccess.Read);  
  32. //pass the filestreanm as object for xmlnode load event  
  33. xmldoc.Load(fs);  
  34. //in this line we actuallty find our setting root name which i set it as column name sth look like this  
  35. //<setting>  
  36. //<column>  
  37. //<innernodes>ect....  
  38. //</column>  
  39. //<column>  
  40. //<innernodes>ect....  
  41. //</column>  
  42. //</setting>  
  43. xmlnode = xmldoc.GetElementsByTagName("column");  
  44. for (int i = 0; i <= xmlnode.Count - 1; i++)  
  45. {  
  46. //read the first node of the current column node and set it datagrideview name'  
  47. //and we are going to use it in our code for setting the columns properties  
  48. string columnName = xmlnode[i].ChildNodes.Item(0).InnerText.Trim();  
  49. int width = int.Parse(xmlnode[i].ChildNodes.Item(1).InnerText.Trim());  
  50. string headertext = xmlnode[i].ChildNodes.Item(2).InnerText.Trim();  
  51. int displayindex = int.Parse(xmlnode[i].ChildNodes.Item(3).InnerText.Trim());  
  52. Boolean visible = Convert.ToBoolean(xmlnode[i].ChildNodes.Item(4).InnerText.Trim());  
  53. //after finding and perparing data now i set the grid properties  
  54. //set the witdh  
  55. dgv.Columns[columnName].Width = width;  
  56. //set the headertext  
  57. dgv.Columns[columnName].HeaderText = headertext;  
  58. //set the column index it means the order of the column  
  59. dgv.Columns[columnName].DisplayIndex = displayindex;  
  60. //set the visibility  
  61. dgv.Columns[columnName].Visible = visible;  
  62. }  
  63. fs.Close();  
  64. }  
  65. //on the closing event of the form I save the grid setting  
  66. //in this exampl I send the xml file name as parmeter so the function the xml file in this name  
  67. //and in this exampl I save the xml file in my f drive but you can create your own folder if you desire  
  68. public void WriteGrideViewSetting(DataGridView dgv, string FileName)  
  69. {  
  70. //declaring teh xmlwritter object  
  71. XmlTextWriter settingwriter = new XmlTextWriter("f:\\XML\\" + FileName + ".xml", null);  
  72.   
  73. //starting the root node which in this example i set the gridview name for this starting node  
  74. //<setting>  
  75. //<column>  
  76. //<innernodes>ect....  
  77. //</column>  
  78. //<column>  
  79. //<innernodes>ect....  
  80. //</column>  
  81. //</setting>  
  82. // the line blow declare the setting tag of the current example and specify the name for the this tag  
  83. settingwriter.WriteStartDocument();  
  84. settingwriter.WriteStartElement(dgv.Name);  
  85. int count = dgv.Columns.Count;|  
  86. //count the gridview column  
  87. for (int i = 0; i < count; i++)  
  88. {  
  89. //now create the column root node  
  90. settingwriter.WriteStartElement("column");  
  91. //then creat the name node and fill the value in this node  
  92. settingwriter.WriteStartElement("Name");  
  93. settingwriter.WriteString(dgv.Columns[i].Name);  
  94. // close the name node  
  95. settingwriter.WriteEndElement();  
  96. //these three node are declared similar to previous node  
  97. settingwriter.WriteStartElement("width");  
  98. settingwriter.WriteString(dgv.Columns[i].Width.ToString());  
  99. settingwriter.WriteEndElement();  
  100. settingwriter.WriteStartElement("headertext");  
  101. settingwriter.WriteString(dgv.Columns[i].HeaderText);  
  102. settingwriter.WriteEndElement();  
  103. settingwriter.WriteStartElement("displayindex");  
  104. settingwriter.WriteString(dgv.Columns[i].DisplayIndex.ToString());  
  105. settingwriter.WriteEndElement();  
  106. settingwriter.WriteStartElement("visible");  
  107. settingwriter.WriteString(dgv.Columns[i].Visible.ToString());  
  108. settingwriter.WriteEndElement();  
  109. //end the column node  
  110. settingwriter.WriteEndElement();  
  111. }  
  112. //end the main root of the xml file which is datagrid name  
  113. settingwriter.WriteEndElement();  
  114. //end the settingwritter  
  115. settingwriter.WriteEndDocument();  
  116. //the close the wriiter  
  117. settingwriter.Close();  
  118. }  
  119. private void Form1_Load(object sender, EventArgs e)  
  120. {  
  121. //initialize the datagridview setting from the xml file  
  122. ReadDataGridViewSetting(dataGridView1, "firstgrid");  
  123. }  
  124. private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
  125. {  
  126. //wriite the datagide view setting  
  127. WriteGrideViewSetting(dataGridView1, "firstgrid");  
  128. }  
  129. }  
  130. }