Click here to Skip to main content
14,773,758 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have added right click menu for DGV. The menu has cell/text color changing feature.
But when I close my application and re-open it, all my cell styles return to default. So, I tried to save those information to an xml file. I created a class for writing xml as follows:
namespace DataGridView
{
    class DataGridViewColor
    {
        public static void WriteDataGridViewSettings(System.Windows.Forms.DataGridView dgv)
        {
            XmlTextWriter writer = new XmlTextWriter(Application.StartupPath + @"\MyGridColor.xml", null);
            writer.WriteStartDocument();
            writer.WriteStartElement(dgv.Name);
            int LastRow = dgv.Rows.Count-1;
            for (int i = 0; i < LastRow; i++)
            {
                for (int j = 0; j < dgv.Columns.Count - 1; j++)
                {
                    writer.WriteStartElement("CellColor");
                    writer.WriteString(dgv.Rows[i].Cells[j].Style.BackColor.ToString());
                    writer.WriteEndElement();
                    writer.WriteStartElement("TextColor");
                    writer.WriteString(dgv.Rows[i].Cells[j].Style.ForeColor.ToString());
                    writer.WriteEndElement();
                }
            }
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
        }

    }
}

I don't know how to read the information from my xml file and put then in DataGridView. Please help me to write reading class.

What I have tried:

I tried the following class for reading (It is not complete):
public static void ReadDataGridViewColor(System.Windows.Forms.DataGridView dgv)
       {
           XmlDocument xmldoc = new XmlDocument();
           XmlNodeList xmlnode;
           FileStream fs = new FileStream(Application.StartupPath + @"\MyGridColor.xml", FileMode.Open, FileAccess.Read);
           xmldoc.Load(fs);
           xmlnode = xmldoc.GetElementsByTagName("column");
Posted
Updated 9-Jan-21 8:57am

1 solution

You can also use XElement.Load(@"MyGridColor.xml"), see:
How to load XML from a file - LINQ to XML | Microsoft Docs[^]
And: Reading XML documents using LINQ[^]

Example:
public static void WriteDataGridViewSettings(System.Windows.Forms.DataGridView dgv)
{
	XmlTextWriter writer = new XmlTextWriter(Application.StartupPath + @"\MyGridColor.xml", Encoding.UTF8);
	writer.WriteStartDocument();
	writer.WriteStartElement(dgv.Name);
	int LastRow = dgv.Rows.Count-1;
	
	for (int i = 0; i < LastRow; i++)
	{
		writer.WriteStartElement("Row");

		for (int j = 0; j < dgv.Columns.Count - 1; j++)
		{
			writer.WriteStartElement("CellColor");
			// This will only work if you use named colors
			writer.WriteString(dgv.Rows[i].Cells[j].Style.BackColor.Name);
			writer.WriteEndElement();
			writer.WriteStartElement("TextColor");
			writer.WriteString(dgv.Rows[i].Cells[j].Style.ForeColor.Name);
			writer.WriteEndElement();
		}

		writer.WriteEndElement();
	}

	writer.WriteEndElement();
	writer.WriteEndDocument();
	writer.Close();
}


Then you can read the file simply like this:
XElement xml = XElement.Load("MyGridColor.xml");

Optional: to produce a more nicely formatted output to the xml file you can use:
writer.WriteWhitespace("\n");
See example here: https://www.dotnetperls.com/xmltextwriter[^]
   
v5
Comments
Alex Dunlop 12-Jan-21 11:49am
   
Please guide me by giving a code so I can study and learn.

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