Developer Express: How to dynamically control Column Display in XtraGridView

Source: Internet
Author: User

XtraGridView has a SaveLayoutToXml method to save the parameter settings for the entire GridView as XML. We modify XML through operations to control the corresponding attribute settings of XtraGridView.

Today, we will list the columns that control the display. In the modified XML file, XtraGridView has a RestoreLayoutFromXml method to load it. The SaveLayoutToXml method is used to export the modified XML file. The two methods

For more information, see the help documentation on the official website. The address is. This website is a good place to learn the Developer Express control. You can see more problems in shipping, Demo, and check the website, as well as the official forum. You have encountered problems before, the above is good. You can also look for it in the blog garden.

Let's get down to the truth. With these two methods, we can operate on the properties of XtraGridView as we like when running the program. The Demo and code are provided below:

Explain the above idea. The column on the large XtraGridView below is created during the design and displayed on it. To retrieve the initialization Xml, use the SaveLayoutToXml method to save the column information of the XtraGridView as XML

File, the small XtraGridView above the read XML is to parse the generated XMl file and bind it to the data source to the small XtraGridView, as shown above, then, we will operate on the XtraGridView. After the operation is complete, we will save the modified modification to the XML file through the Save modification button. In this Demo, the small one will only modify the Visble attribute, in this way, you can define the columns to be displayed. After the modified xml file is loaded, the RestoreLayoutFromXml method is used. For example:

This is the result. The younger brother is not talented. What's worse? Please give me some advice. I want to make a record for my friends who have encountered this problem. The so-called good memory is not as bad as a pen. The following code is pasted:

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 ("Operation Succeeded ");} 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 ("Visib LeIndex ", 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 nodeChile Ds 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 the modification {XmlDocument objXmlDoc = new XmlDocument (); path = System. appDomain. currentDomain. setupInformation. applicationB Ase + "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 ("N Ame ", 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 = Conv Ert. 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 );}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.