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