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.
- 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.
- 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.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- //these two namespace are required
- using System.Xml;
- using System.IO;
- //......
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace GridSetting
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- //function which read the seting from the xml file
- public void ReadDataGridViewSetting(DataGridView dgv, string FileName)
- {
- //declare the xmldocument object
- XmlDocument xmldoc = new XmlDocument();
- //and also xmllistnode
- XmlNodeList xmlnode;
- //declare the filestream for reading and accessing the xml file
- FileStream fs = new FileStream("f:\\XML\\" + FileName + ".xml", FileMode.Open,FileAccess.Read);
- //pass the filestreanm as object for xmlnode load event
- xmldoc.Load(fs);
- //in this line we actuallty find our setting root name which i set it as column name sth look like this
- //<setting>
- //<column>
- //<innernodes>ect....
- //</column>
- //<column>
- //<innernodes>ect....
- //</column>
- //</setting>
- xmlnode = xmldoc.GetElementsByTagName("column");
- for (int i = 0; i <= xmlnode.Count - 1; i++)
- {
- //read the first node of the current column node and set it datagrideview name'
- //and we are going to use it in our code for setting the columns properties
- string columnName = xmlnode[i].ChildNodes.Item(0).InnerText.Trim();
- int width = int.Parse(xmlnode[i].ChildNodes.Item(1).InnerText.Trim());
- string headertext = xmlnode[i].ChildNodes.Item(2).InnerText.Trim();
- int displayindex = int.Parse(xmlnode[i].ChildNodes.Item(3).InnerText.Trim());
- Boolean visible = Convert.ToBoolean(xmlnode[i].ChildNodes.Item(4).InnerText.Trim());
- //after finding and perparing data now i set the grid properties
- //set the witdh
- dgv.Columns[columnName].Width = width;
- //set the headertext
- dgv.Columns[columnName].HeaderText = headertext;
- //set the column index it means the order of the column
- dgv.Columns[columnName].DisplayIndex = displayindex;
- //set the visibility
- dgv.Columns[columnName].Visible = visible;
- }
- fs.Close();
- }
- //on the closing event of the form I save the grid setting
- //in this exampl I send the xml file name as parmeter so the function the xml file in this name
- //and in this exampl I save the xml file in my f drive but you can create your own folder if you desire
- public void WriteGrideViewSetting(DataGridView dgv, string FileName)
- {
- //declaring teh xmlwritter object
- XmlTextWriter settingwriter = new XmlTextWriter("f:\\XML\\" + FileName + ".xml", null);
- //starting the root node which in this example i set the gridview name for this starting node
- //<setting>
- //<column>
- //<innernodes>ect....
- //</column>
- //<column>
- //<innernodes>ect....
- //</column>
- //</setting>
- // the line blow declare the setting tag of the current example and specify the name for the this tag
- settingwriter.WriteStartDocument();
- settingwriter.WriteStartElement(dgv.Name);
- int count = dgv.Columns.Count;|
- //count the gridview column
- for (int i = 0; i < count; i++)
- {
- //now create the column root node
- settingwriter.WriteStartElement("column");
- //then creat the name node and fill the value in this node
- settingwriter.WriteStartElement("Name");
- settingwriter.WriteString(dgv.Columns[i].Name);
- // close the name node
- settingwriter.WriteEndElement();
- //these three node are declared similar to previous node
- settingwriter.WriteStartElement("width");
- settingwriter.WriteString(dgv.Columns[i].Width.ToString());
- settingwriter.WriteEndElement();
- settingwriter.WriteStartElement("headertext");
- settingwriter.WriteString(dgv.Columns[i].HeaderText);
- settingwriter.WriteEndElement();
- settingwriter.WriteStartElement("displayindex");
- settingwriter.WriteString(dgv.Columns[i].DisplayIndex.ToString());
- settingwriter.WriteEndElement();
- settingwriter.WriteStartElement("visible");
- settingwriter.WriteString(dgv.Columns[i].Visible.ToString());
- settingwriter.WriteEndElement();
- //end the column node
- settingwriter.WriteEndElement();
- }
- //end the main root of the xml file which is datagrid name
- settingwriter.WriteEndElement();
- //end the settingwritter
- settingwriter.WriteEndDocument();
- //the close the wriiter
- settingwriter.Close();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- //initialize the datagridview setting from the xml file
- ReadDataGridViewSetting(dataGridView1, "firstgrid");
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- //wriite the datagide view setting
- WriteGrideViewSetting(dataGridView1, "firstgrid");
- }
- }
- }

Yan Poletto AngulskiPosted Nov 23, 2019, 2:56 AM
AND THE COLORS? U DONT SAVE THE COLORS.
MiklosPosted Mar 30, 2018, 5:11 AM
Thanks, great code
Zubair ahamed.mPosted Aug 24, 2017, 5:25 AM
Thanks, my pro solved, u r grate
krishan kumar singhPosted Jun 22, 2017, 7:18 AM
Thanks, Good login, My problem resolved but only changed this code if file is not exists then write on page load
Shabbir HusainPosted Apr 27, 2017, 1:27 AM
There is no " firstgrid.xml " file in zip file.....
Shane LaneyPosted Apr 12, 2016, 3:04 PM
Change the method calls to look like this: ReadDataGridViewSetting(dataGridView1, "firstgrid.xml"); The file load and save commands would look like this: FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read); And you could add a conditional to check if the file exists when loading: if (File.Exists(FileName))
Shane LaneyPosted Apr 12, 2016, 3:01 PM
So, may I suggest the following changes.
Shane LaneyPosted Apr 12, 2016, 3:01 PM
Your code doesn't run as provided in the zip file because you have hardcoded the filename to an F: drive. Also, the filename isn't really the file name as it is only part of the filename:)
Behzad BabaeiPosted Jul 29, 2014, 4:36 PM
thanks for comment but if you have any idea or suggestion to make this article better plesae comment it I'll be very gratefull thanks.
Shawpnendu Bikash MaloroyPosted Jul 29, 2014, 2:44 PM
Good to read