Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Objective Grid for Microsoft .NET User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

3.7 Extending .NET Functionality

Objective Grid for Microsoft .NET is a wrapper over the Objective Grid MFC libraries. It is easy to customize the libraries to implement the functionality you need. This section provides several examples of customization.

3.7.1 Customizing Behavior

You can customize the behavior of your applications in a number of ways. This section includes some examples.

3.7.1.1 Accepting Numeric Data Only

You can limit the data in a cell to accept only numeric data. For example:

3.7.1.2 Highlighting a Value in a Cell

To highlight the value in a cell, add the following function to the file GridControl.h:

In the source file GridControl.cpp, add:

After rebuilding the assemblies, you can use code similar to the following:

3.7.1.3 Advancing the Cursor from the Last Column to the Next Row's First Column

To advance a cursor from the last column in one row, to the first column of the next when the user presses either the right arrow, enter, or tab key, you can create a custom event handler. For example:

In most cases, the above works. However, when a cell is a combo box (for instance, style.Control = ControlType.DropDownList;), the enter/tab/left arrow/right arrow keys won't advance to the next row. Here is a workaround:

3.7.1.4 Extending the Last Column to Infinity When Scrolling Horizontally

You can extend the last column to infinity during horitontal scrolling using this code:

3.7.1.5 Setting the Max and Min Date in a DateTime Control

Here is an example for setting the Max and Min date values in a DateTime control:

3.7.1.6 Making the Current Cell Inactive

To deactivate the current cell in MFC version of Grid, we can use the following:

We can implement the same feature in Grid for .NET, changing the source code in file gridcontrol.h:

After rebuilding the libraries, you can use the following in your program code:

3.7.1.7 Retrieving the Date from a DateTime Control

You can use the function GetValueRowCol() to return the date from a cell with a DateTime control, rather than retrieving just the numeric value.

For example, the following function returns a System.DateTime object:

3.7.1.8 Tabbing to a Grid for .NET Control on a Form

When tabbing through all the controls on a Windows Form, the Grid Control may not receive focus (i.e. may be skipped) even though the Grid Control's TabIndex and TabStop properties are set correctly. The following source code works around this issue in the Windows Form application. All changes below refer to the application's Windows Form *.cs file containing the Grid Control along with other .NET controls.

In the System.Windows.Forms.Form derived, public class, add the following private, initialized boolean variable:

In the Windows Form Designer generated class private function InitializeComponent(), under the Grid Control section add the following event handler:

After the Windows Form Designer generated class region, add the following protected override and private delegate:

Rebuild the application to test the tab and shift+tab traversal and focus of each indexed control on the Windows Form.

3.7.1.9 Adding a Cut Event Handler for Use Within a Virtual Grid

To extract characters from a cell within a Virtual Grid, add a Cut event handler within the Grid Control as shown below:

For information on the setSel() function, see Section 3.7.1.2, "Highlighting a Value in a Cell."

3.7.1.10 Loading OGL Files

You can load OGL files in an application that uses the Objective Grid for Microsoft .NET component, like so:

This feature is implemented in <stingray-installdir>\Objective Grid for Microsoft .NET\Tutorials\1stGrid\Step 2.

3.7.2 Customizing the Display

This section discusses ways in which you can customize how your application is displayed to users.

3.7.2.1 Displaying Ellipses for Values Greater than a Column Width

You can display ellipses if the text in a column is greater than the column's width. To enable the use of ellipses:

For example:

3.7.2.2 Customizing the Display of Wide Grids that Flow off the Page

For very wide grids in which right-hand columns are not fully displayed, default Objective Grid for Microsoft .NET behavior is to shift the entire grid to the left when a user clicks in a far right-hand column, in order to allow its display.

You can disable this behavior by returning FALSE in CGXDynRegularGrid<T>::ScrollCellInView, like this:

Finally, rebuild GridControl.

3.7.2.3 Displaying the Time

By default, the style.control DateTime displays only the date, but you can also easily display the time. The format of the DateTime control is set by the UserAttribute (see the GridApp sample provided for an MFC Grid) as in the following code snippet:

3.7.2.4 Changing the Color of a Cell's Top Border

To change the color of a cell's top border, edit the code in the file Stingray Studio\Objective Grid for Microsoft.NET\Solutions\GridControl\Style\Style.h, to add the code block between the comments, like this:

After rebuilding the assemblies, you can do the following:

3.7.2.5 Setting an Interior Color for a Button Control

Set an interior color for a button control like this:

3.7.2.6 Hiding Columns

To prevent the user from resizing hidden columns, use code like this:

3.7.2.7 Setting the Font in a Range of Cells to Bold

To set the font in a range of cells to bold:

3.7.2.8 Making a Combobox Visible in an Inactive Cell

If you wish to make combobox button visible in inactive cell, add this code to the file GridControl.h:

After rebuilding the assemblies, you can use this code like so:

3.7.2.9 Formatting a Numeric Cell Using Currency Control and UserAttributeIDs

UserAttributeIDs are enumeration types that marshall the MFC Grid style types used by the Objective Grid for Microsoft .NET grid control for the formatting of numeric cells.

The Objective Grid for Microsoft .NET UserAttributeIDs can be found in <stingray-install-dir>\Objective Grid for Microsoft .NET\Solutions\GridControl\Constants\OGEnumsAndConstants.h.

The marshalling of these enumeration types can be found in <stingray-install-dir>\Objective Grid for Microsoft .NET\Solutions\GridControl\Style\Style.h and Style.cpp.

Objective Grid for Microsoft .NET control provides a Currency control, Stingray.Grid.ControlType.Currency, which is flexible and easy to use.

Below are explanations and usage examples of some of these .NET UserAttributeIDs as used by the Grid Control's Currency control. Each item contains code snippets from the larger code block below illustrating the use of UserAttributeIDs to format numeric cells in various ways.

The comments in the larger code block reference the code snippet by item number for readability and ease of use.

  1. UserAttributeID.CURUMDECIMALS: Setting the Number of Decimal Places for Numbers

    The end user should be able to change the number of decimal places at runtime.

    For example, if the end user enters a value of 1000.00002 and the current format setting for the decimal place is set to 2, then the numeric value should be displayed as 1000.00, while the grid's cell maintains the actual value of 1000.00002.

    Required Event Handlers

    Note that event handlers for Start and End editing are required for correct runtime behavior. These event handlers are implemented as shown:

  2. UserAttributeID.CURNEGFORMAT:Changing the Display of Negative Numeric Values

    The end user should be able to change how negative numbers are displayed at runtime.

    For example, if an end user enters -22.22, the numeric value should be displayed as (22.22), while the grid's cell maintains the actual value of -22.22.

  3. UserAttributeID.CURSEP: Setting the Thousand Separator Delimiter Character

    The end user should be able to change the number of decimal places at run time.

    For example, if the end user enters a value of 10000, the numeric value should be displayed as 10,000, while the grid's cell maintains the actual value of 10000.

  4. UserAttributeID.CURMON: Constraining the Input of Valid Numeric Data Only

    The end user should be able to enter only valid numeric data in any given cell at runtime.

    For example, the end user should be able to enter only numeric values in any given cell or range of cells with this attribute.

    If the end user tries to enter some other non-numeric character, the cell should ignore that character.

    Please refer to the section, UserAttributeID Enumeration, for a complete list of attributes and as listed in the Objective Grid for Microsoft® .NET® Reference Guide.

    Please refer to the MFC Objective Grid's User's Guide and Reference Guide for details of the style types called from the MFC Objective Grid and marshalled by the Objective Grid for Microsoft .NET grid control.

3.7.2.10 Matching Background Colors in a Grid

You may change the background color of all cells in the grid. But to change the background color of the whole grid, so that the background color of the cells and the remaining area of the grid matches, you must make additional changes.

In the MFC Grid, use the following function:

and then set nIndex, like this: nIndex = GX_COLOR_BACKGROUND.

In Objective Grid for Microsoft .NET, add the following code into public __gc class OGProperties in the OGProperties.h file, located in the ..\Solutions\GridControl\Param folder, as shown here:

After rebuilding the assemblies, you can access this property from your C# code. After these changes, you can use the following code in your project:

3.7.3 Printing

This section describes how to set print margins and enable color printing.

3.7.3.1 Setting Print Margins

In MFC Grid, use the following functions to set the print margins (left, right, top, bottom):

To set print margins with Objective Grid for Microsoft .NET using VC#, add the following code into public __gc class OGProperties in the OGProperties.h file, located in the ..\Solutions\GridControl\Param directory:

Rebuild the solution from the folder, ..\Solutions\GridControl. With the new assemblies, you will be able to call these functions in your project.

3.7.3.2 Enabling Color Printing

To enable/disable color printing for Objective Grid using VC#, add the following code into public __gc class OGProperties in the OGProperties.h file, located in the ..\Solutions\GridControl\Param directory:

After rebuilding the assemblies, you will be able to access this property from your C# code.

3.7.3.3 Grid Line Display and Printing

Even if you are only displaying some cell grid lines, Objective Grid for Microsoft .NET may show grid lines for all cells in Print Preview and print them all as well. Exposing the following functions from the MFC base code to C# (similar to the approach demonstrated in Section 3.7.3.1) should resolve this problem:

3.7.4 Sorting

You can customize the behavior and display when sorting, discussed in this section.

3.7.4.1 Marking the Sort Column

You can mark the sorting column with a small triangle in the header, using the following code:

3.7.4.2 Sorting with Empty Cells

The code below sorts the values of a column when that column is double-clicked:

For columns that lack a value in any row, you need to edit the code in the file gxDynRegularGrid.h and then rebuild the control, or attempts to sort the column result in a debug assert.

For example:

3.7.5 Memory and Performance Issues

This section provides tips and troubleshooting when dealing with memory or performance issues.

3.7.5.1 Performance Issues when Displaying Numbers

There are a few ways to set values in Objective Grid for Microsoft .NET: using property Style.Value or functions SetValueRange() and SetStyleRange(). The test in this section was used in the 1stGrid Tutorial sample to compare performance and speed for these methods.

Using LockUpdate(true) and LockUpdate(false) around the code of interest increases performance.

Add this code to the application's WinForm to perform benchmark testing:

A menu option or button control may be added to the application's WinForm to perform benchmark tests.

For example, the button click event below was added to show how to benchmark test three different approaches when setting numeric values in the grid.

Test Results:

Benchmark test results without the use of LockUpdate(true) and LockUpdate(false):

Benchmark test results with the use of LockUpdate(true) and LockUpdate(false):

3.7.6 Troubleshooting

This section includes possible issues you may encounter and methods for resolving them.

3.7.6.1 Releasing Memory

This section provides a workaround if an application appears to increase in memory usage.

Add these two two lines of code anywhere in your application where memory should be released.

A similar approach works with C#, where you can add this line at the top of the application's Form.cs file:

Then add this code block to the application's WinForm class, public class Form1 : System.Windows.Forms.Form:

Finally, call these two lines of code anywhere in your application where memory should be released.



Previous fileTop of DocumentContentsNo linkNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and Stingray, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.