FlexGrid for WinForms
Step 2 of 5: Read the Data and Build the Outline
Show AllShow All
Hide AllHide All

To read the data and build the outline, add code to the Button1_Click event and add the GetXMLData routine.

  1. Double-click the command button and add the following code to the Button1_Click event:

    To write code in Visual Basic

    To write code in C#

    Observe the following:

    The routine starts by showing an OpenFileDialog that allows the user to select an XML file to load into the grid. When the file is selected, the routine loads it into an XmlDocument object, which parses the contents of the file into memory.

    The routine then sets the grid's Redraw property to False to suspend repainting while the control is populated. This technique improves performance significantly, and you should always use it when adding substantial amounts of data to the C1FlexGrid.

    Next, the routine clears any data by setting Count to 1, and calls the GetXMLData routine to populate the control with the contents of the XmlDocument. The GetXMLData routine is the main one in this tutorial, and is listed below.

    After the grid has been populated, the routine uses the AutoSizeCol method to adjust the width of the first column based on its contents, and the Show method to expand the outline and show levels 0, 1, and 2. The routine then sets the Redraw property back to True so the grid starts repainting normally.

  2. The GetXMLData routine is the most interesting one in this tutorial. It traverses the XMLDocument object and builds the outline tree. Add the following code to the form:

    To write code in Visual Basic

    To write code in C#

Observe the following:

The routine starts by skipping XML comment nodes. Then it uses the Rows.Add method to add a new row to the grid.

Next, the routine sets the node name and checks whether the node has exactly one child. In this case, the node is interpreted as a data node, and the node's InnerText property is copied to the second column on the new row. The code also sets the style of cells containing data to the custom style "Data" created when the form was loaded.

The next block of code checks to see whether this node has a subnode called "Name". If it does, then the contents of the "Name" node are assigned to the new row's UserData property. This value will be used later to implement ToolTips, so users can see the node name even when it is collapsed.

Finally, if the node has children, the GetXMLData routine calls itself to add the child nodes to the grid as well.

Run the program and observe the following:

The project can load XML files and display them, and the user can collapse and expand nodes by clicking on them.


The next steps add a few improvements to make the application easier to use.

See Also

 

 


Copyright © GrapeCity, inc. All rights reserved.

Product Support Forum |  Documentation Feedback