FlexGrid for WinForms
Step 2 of 4: Initialize and Populate the Grid
Show AllShow All
Hide AllHide All

To set up the grid and populate the grid with the sales data we want to analyze, set the layout properties and styles either in the designer or in code, and use the GetDataSource method to populate the grid.

  1. Set up the grid layout and styles either in the designer or in code.

    In the Designer

    • In the Properties window, set the following properties:
      Property Setting
      AllowEditing False
      AllowSorting None
      AllowMerging Nodes
      ExtendLastCol True
      SelectionMode Cell
      Tree.Style Simple
      Tree.Column 1
    • Open the C1FlexGrid Style Editor by selecting Styles from the C1FlexGrid Tasks menu. For more information on accessing the C1FlexGrid Style Editor, see Accessing the C1FlexGrid Style Editor.
    • Select Normal from the list of Built-In Styles.
    • Set the Border.Style property to None and the Trimming property to EllipsisCharacter.
    • Select Subtotal0 from the list of Built-In Styles.
    • Set the BackColor property to Gold, and the ForeColor property to Black.
    • Set the properties for Subtotal1 and Subtotal2 to the following:
      Subtotal1
      BackColor Khaki
      ForeColor Black
      Subtotal2
      BackColor LightGoldenrodYellow
      ForeColor Black
    • Click OK to close the editor.

    In Code

    Add the following code to the Form_Load event to set up the grid layout and styles:

    To write code in Visual Basic

    To write code in C#

    The routine starts by setting up the grid layout and some styles.

  2. Bind C1FlexGrid to a data source by adding the following code to the Form_Load event:

    To write code in Visual Basic

    To write code in C#

    The routine binds it to a data source created by the GetDataSource method, listed below.

  3. Lock the last three columns in place by setting their AllowDragging property to False. This is done to prevent the user from grouping the data in these columns (the values in these columns are distinct for each row). This property can be set either in the designer or at in code.

    In the Designer

    • Select Column 4 in the grid. This will open the Column Tasks menu for Column 4.
    • Uncheck the Allow Dragging check box.
    • Repeat for Column 5 and Column 6.

    Alternatively, the AllowDragging property can also be set using the C1FlexGrid Column Editor:

    • Open the C1FlexGrid Column Editor by selecting Designer in the C1FlexGrid Tasks menu. For details on how to access the C1FlexGrid Column Editor, see Accessing the C1FlexGrid Column Editor.
    • Select Column 4 in the right pane.
    • In the left pane, set the AllowDragging property to False.
    • Set the AllowDragging property to False for Column 5 and Column 6.
    • Do not close the editor.

    In Code

    Add the following code to the Form_Load event:

    To write code in Visual Basic

    To write code in C#

  4. Set the Format property of the Sales Amount column so that the amounts are displayed as currency values. This can be done either in the designer or in code.

    In the Designer

    • Select Column 6 in the grid.
    • Click the ellipsis button in the Format String box to open the Format String dialog box.
    • Under Format type select Currency.
    • Click OK to close the Format String dialog box.

    Alternatively, the Format property can also be set using the C1FlexGrid Column Editor:

    • In the C1FlexGrid Column Editor, select Column 6 in the right pane.
    • In the left pane, click the ellipsis button next to the Format property to open the Format String dialog box.
    • Under Format type select Currency.
    • Click OK to close the Format String dialog box.
    • Click OK to close the editor.

    In Code

    To write code in Visual Basic

    To write code in C#

  5. The GetDataSource method creates the data table that is displayed by the grid. The routine is very basic, except for the SQL statement that retrieves the data. Most people don't write these SQL statements manually, but use visual designers such as the one in Visual Studio or Microsoft Access to do that.

    Add the following code to the form. Note that you may have to change the connection string slightly, because it has a reference to the NorthWind database and that file might be in a different folder in your system:

    To write code in Visual Basic

    To write code in C#

Run the program and observe the following:

You will see a plain-looking grid that allows you to move columns around and browse through the data. However, the data is not structured in a clear way, and this table contains a couple of thousand records, so it is pretty difficult to get an overview of what the data means.


See Also

 

 


Copyright © GrapeCity, inc. All rights reserved.

Product Support Forum |  Documentation Feedback