Tips 1 and 2: Getting Started
I began this project by creating a custom grid class, and added additional properties and methods to support the functionality discussed. I also sub-classed other basic controls (TextBox, Label, CheckedListBox) that I'll use in this project. All of these can be found in ccControls.cs
. Table 1 lists all of the properties and methods used by the grid class.
Table 1: Exposed properties and functions for the Grid class (ccControls.ccGrid).
The Binding Manager for the DataGrid
Flag for whether the current row should be highlighted
The current DataView for the DataGrid
Defines a custom function for setting the grid caption. It's helpful if the caption displays a running summary calculation. Pass the function assembly, function class, and function name. The function is invoked through the reflection library.
Returns the current DataRow from the DataGrid
Defines a custom function that evaluates each column during the Paint Event, and returns a Boolean to indicate whether the column should be displayed in a different color. Pass the following:
- Column Name to be evaluated (string)
- Function Assembly (string)
- Function Class Name (string)
- Function Name (string)
- Four sets of Color RGB to use for column display if the function returns a True (uses Gradient Fill)
- Background Color From, Background Color To
- Foreground Color From, Foreground Color To
Performs a text search based on a certain string, either starting at the top of the grid, or one row beyond the current position. Utilizes the scoped columns if that setting is on.
Marks the FilterSelected Column as True for each DataRow that matches the textsearch (uses the same rules as the textsearch)
Initializes the grid
Adds a textbox column to the grid
Adds a date column to the grid
Adds a checkbox column to the grid
Adds a drop-down column to the grid
Adds a textbox column to the grid (where the text comes from a column from a target relation)
Returns a column object, based on the name of the column passed as a string
The last function to be called just prior to displaying the DataGrid. Pass the name of the DataSource as a parameter
|I had been using the grid class in ccControls for quite some time when realized that I was duplicating certain controls necessary for things like Find and Find Next.|
I had been using the grid class in ccControls for quite some time when realized that I was duplicating certain controls necessary for things like Find and Find Next. Any time I find myself doing a copy/paste, my internal control radar goes off! So I created a UserControl class that contained my custom grid control and all the controls necessary for the other operations that I'll discuss later. This UserControl class is called ccGridContainer and is found in ccContainers.cs
After building ccControls and ccContainers as DLLs, I added ccContainers to my Visual Studio .NET IDE Toolbox, and then created a new Windows Forms project. I then dropped an instance of ccGridContainer onto my form, and changed the name of the control from ccGridContainer1 to oGridCtr
. Next, to get an object reference directly to the grid control itself, I added the following bit of code:
ccControls.ccGrid oGrid = oGridCtr.oGrid;
Of course, you can't get very far without creating some test data. I didn't want to complicate matters by introducing a data access layer into this example, so I'll create a method to produce some test data and assume that in a production environment, this data would be returned as a result set from a business object working with a data component.
In the example, an example from a basic accounting application, I present the user with a list of deductions (short-pays) that have been taken by certain customers. The deduction table (DtDeductions
) contains the following:
- Deduction ID
- Customer #
- Reason Code #
- Original Deduction Amount
- Current Deduction Balance
- Whether the deduction has been approved for any activity
There will be two support tables: a customer account table (DtAccounts
) with account descriptions, and a reason code table (DtRsnCode
) with reason code descriptions. The code to create this test data and define data relations can be found in the CreateTestData
function in the main test form.
Finally, I want to define the grid columns. Normally, to define an individual column, you need to do something like this:
Fld1 = new DataGridTextBoxColumn();
Fld1.Alignment = HorizontalAlignment.Left;
Fld1.HeaderText = "Deduction #";
Fld1.MappingName = "Deduction ID";
Fld1.ReadOnly = true;
Fld1.Width = 190;
Fortunately, the new grid class makes life a little simpler when it comes to defining columns. The grid class accomplishes the same thing in one function call:
oGrid.AddText("DeductID", "Deduction #",100,
The parameters for AddTextColumn
- Column name (mapping name)
- Column Heading
- Column Width
- Column Alignment (Left, Center, or Right)
- Read only (True or False)
- Any special formatting (mainly for currency columns)
You might be saying, "OK, but what about other columns? I want to display an account name from a foreign table, and I also want to display the list of reasons from my description table, yet have any changes map to the reason code column of my deduction table. I also want to show a checkbox for the approved flag."
No worries, mate! There are similar functions for each of these. Table 2 lists all the functions to set Grid Columns for different column types. If you are defining a drop-down with a description coming from another table, you'll need to specify the foreign table and description. Similarly, if you're defining a text column with a description from a related table, you'll have to supply the name of the ADO.NET relationship. The sample project has an example of each of these. All functions to set grid columns require Mapping Name
, and Formatting
Table 2: The functions for defining grid columns.
The standard six
The standard six, plus
ADO.NET relation name
Column object for the related column to be displayed
The standard six, plus
Column Name to display
Foreign key name
Foreign table table name
The standard six
The standard six
function in the test form contains an example of each call to define a grid column. As you add columns to the Grid class using these functions, the Grid class holds the column information in a DataTable named DtGridDef
. The column names and a brief description for each can be found in the function InitializeGrid
. Any additional column definitions you'd like to set up can be added to this table.
Once you've made all the calls to SetGridColumns
that you need, simply call the function ShowGrid
, and you'll see the grid appear with the columns you defined!