How XtraGridView of Developer Express dynamically controls the display of columns

Keywords: xml Python Attribute Windows

XtraGridView has a Save LayoutToXml method that saves parameters about the entire GridView as XML. We control the corresponding property settings of XtraGridView by manipulating the XML.

Today we will list the columns of control display. In the XtraGridView of the modified XML file, a RestoreLayoutFromXml method is loaded to export the modified XML file on the SaveLayoutToXml method. On these two methods

Look at the help document of the official website. The address is: http://documentation.devexpress.com/#WindowsForms/DevExpressXtraGridViewsBaseBaseView_SaveLayoutToXmltopic . This website is a good place to learn about the Developer Express control. Look at more problems when you ship it to Demo, check it on the website, and the official forum. The problems you encounter are also encountered by others before. The above ones are still good. It's OK to go to the blog park to find them.

Back to the point, through these two methods, we can manipulate the properties of XtraGridView as we like when the program runs. Demo and code are given below:

Explain the idea above. The column on the bottom big XtraGridView was created and displayed when it was designed. Getting initialized Xml saves the column information of the XtraGridView as XML using the Save LayoutToXml method

File, the reading XML on the small XtraGridView is to parse the Xml file generated above and bind it to the small XtraGridView as shown above. Then we operate on the XtraGridView and save the modification to the XML file by saving the modification button. The small Demo just modifies the Visble attribute. The effect of this is to give people a definition of what columns to display. The RestoreLayoutFromXml method is used here to load the modified XML by loading the XML button. As follows:

That's the effect. The younger brother is not talented, where is not good, please everybody knight-errant more advice. For the friend who encounters this problem to learn from, to make a record of myself, is the so-called good memory is not as bad as bad pen. Here's the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using System.Xml;


namespace WFADynamicallyGeneratedCommlueColumn
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string path= System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "test.xml";
        private void sbtnStact_Click(object sender, EventArgs e)//Get the initialization Xml
        {
            path=cInputBox.InputBox("operation", "File name:", gv1.ViewCaption);
            if (path.Length > 0)
            {
                path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + path;

                OptionsLayoutBase options = new OptionsLayoutBase();
                gv1.SaveLayoutToXml(path, options);
                XtraMessageBox.Show("Successful operation");
            }
            else
            {

            }
        }

        private void sbtnReadXml_Click(object sender, EventArgs e)//Read Xml
        {
            XmlDocument objXmlDoc = new XmlDocument();
            path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "test.xml";
            objXmlDoc.Load(path);
            string tNodeCaption;
            XmlNodeList node = objXmlDoc.SelectSingleNode("//property[@name='Columns']").ChildNodes;

            DataTable dt = new DataTable();
            dt.Columns.Add("VisibleIndex", System.Type.GetType("System.String"));
            dt.Columns.Add("Visible", System.Type.GetType("System.Boolean"));
            dt.Columns.Add("Caption", System.Type.GetType("System.String"));
            dt.Columns.Add("Name", System.Type.GetType("System.String"));
            dt.Columns.Add("FieldName", System.Type.GetType("System.String"));

            
            foreach (XmlNode nodeChiled in node)
            {
                DataRow dr = dt.NewRow();
                foreach (XmlNode nodeChileds in nodeChiled.ChildNodes)
                {
                    tNodeCaption = nodeChileds.Attributes["name"].Value;

                    switch(tNodeCaption)
                    {
                        case "VisibleIndex": dr[0] = nodeChileds.InnerText;
                            break;
                        case "Visible":      dr[1] = Convert.ToBoolean(nodeChileds.InnerText)?true:false;
                            break;
                        case "Caption":      dr[2] = nodeChileds.InnerText;
                            break;
                        case "Name":         dr[3] = nodeChileds.InnerText;
                            break;
                        case "FieldName":    dr[4] = nodeChileds.InnerText;
                            break;
                       
                    }
                }
                dt.Rows.Add(dr);
            }
            gc2.DataSource = dt;
        }

        private void saveChang_Click(object sender, EventArgs e)//Save Modifications
        {
            XmlDocument objXmlDoc = new XmlDocument();
            path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "test.xml";
            objXmlDoc.Load(path);
            string tNodeCaption;
            XmlNodeList node = objXmlDoc.SelectSingleNode("//property[@name='Columns']").ChildNodes;

            DataTable dt = new DataTable();
            dt.Columns.Add("VisibleIndex", System.Type.GetType("System.String"));
            dt.Columns.Add("Visible", System.Type.GetType("System.Boolean"));
            dt.Columns.Add("Caption", System.Type.GetType("System.String"));
            dt.Columns.Add("Name", System.Type.GetType("System.String"));
            dt.Columns.Add("FieldName", System.Type.GetType("System.String"));

           dt =(DataTable)gc2.DataSource;
           int i = 0;
            foreach (XmlNode nodeChiled in node)
            {
                DataRow dr = dt.Rows[i];
                foreach (XmlNode nodeChileds in nodeChiled.ChildNodes)
                {
                    tNodeCaption = nodeChileds.Attributes["name"].Value;

                    switch (tNodeCaption)
                    {
                        case "VisibleIndex": nodeChileds.InnerText =Convert.ToString(dr[0]);
                            break;
                        case "Visible":      nodeChileds.InnerText =Convert.ToBoolean( dr[1])?"true":"false";
                            break;
                        case "Caption": nodeChileds.InnerText = Convert.ToString(dr[2]);
                            break;
                        case "Name":         nodeChileds.InnerText =Convert.ToString( dr[3]);
                            break;
                        case "FieldName":    nodeChileds.InnerText =Convert.ToString(dr[4]) ;
                            break;

                    }
                }
                if (i < dt.Rows.Count)
                    i++;
            }
            objXmlDoc.Save(path);
            
        }

        private void sbtnLoadXml_Click(object sender, EventArgs e)//Load Xml
        {
            gv1.RestoreLayoutFromXml(path);
        }
    }
}

Reproduced in: https://my.oschina.net/cookblack/blog/621397

Posted by andy666 on Fri, 14 Jun 2019 17:08:54 -0700