WinGrid Runtime Config and Persistence

Tom Puglisi / Thursday, January 26, 2012

Ever wanted to create a WinForms app that uses WinGrid and expose the powerful control API to your end users so that they can totally customize columns, add formulas, style the look and feel, even add, remove and hide columns, or JUST ABOUT ANYTHING that you can typically do in Microsoft™ Visual Studio though the Property Window? Well, check out this blog post and you CAN!

 

In this sample (feel free to download it), I have the Infragistics NetAdvantage Windows Forms WinGrid control along with a Microsoft Property Grid control thrown onto the form. I am also using the NetAdvantage WinTilePanel control to allow you to move the various controls around the screen however you like. The Property Grid control is hooked up to the WinGrid control’s DisplayLayout property. The DisplayLayout property essentially contains ALL of the WinGrid control’s important property settings related to its functionality, behaviors, features, schema, and so forth.

With this kind of setup, you can run the application and then play around with the various property settings at runtime. However, the awesome part is:

With just a few lines of code, you can SAVE whatever changes you make to WinGrid and then LOAD them up later on. That’s the part that I LIKE

 

This is the form layout along with the controls:

 

The following code shows the simplicity of LOADING and SAVING this particular WinGrid control’s settings:

        string _settings = Application.StartupPath + @"\settings.xml";

        private void Form1_Load(object sender, EventArgs e)
        {
            //Load Settings
            if (File.Exists(_settings))
            {
                this.ultraGrid1.DisplayLayout.LoadFromXml(
                    _settings, 
                    Infragistics.Win.UltraWinGrid.PropertyCategories.All);
            }
            //Get Data
            this.ordersTableAdapter.Fill(this.nwindDataSet.Orders);
            this.order_DetailsTableAdapter1.Fill(this.nwindDataSet.Order_Details);
            
            //Set PropertyGrid to WinGrid's DisplayLayout property
            this.propertyGrid1.SelectedObject = this.ultraGrid1.DisplayLayout;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Save Layout
            this.ultraGrid1.DisplayLayout.SaveAsXml(
                _settings, 
                Infragistics.Win.UltraWinGrid.PropertyCategories.All);
        }

When you download and run this sample, you can observe the various columns that have been added to the BAND[0].Columns collection as well as the BAND[1].Columns collection. I added two unbound columns and set their Formula property through the WinCalcManager component’s Formula Editor. That’s right! I also have the NetAdvantage WinCalcManager component thrown onto the form so that even at runtime, I can use the component’s ability to customize WinGrid by adding as many additional empty columns as I like, while setting the Column object’s Formula property to evaluate as many as 140 Excel-like functions that are at your disposal. You can also provide WinCalcManager-powered summaries that aggregate column data. As a matter of fact, the ROOT entity in WinGrid has an unbound column that actually displays the SUM of its child order item’s TOTAL column.

 

Try this: after playing around with the sample, DELETE the settings.xml file from the sample’s BIN folder. Now run it again. The WinGrid control will be reset. All of MY customizations that were saved are now blown away.

 

Now that you have seen this sample and got the idea of how it works, you can apply the same or similar concepts to your larger-scale apps as well. Remember that if you use the Microsoft Property Grid control, you may scare some of your end users, but you can carefully target just PARTS of the WinGrid object model in order to minimize your end users BREAKING stuff!

Enjoy!