Working with mmDataGrid


Note: mmDataGrid is based on the old .NET 1.1 DataGrid. We recommend using the MM .NET mmDataGridView based on the new .NET 2.0 DataGridView control instead.

The MM .NET Windows Forms mmDataGrid class has a variety of settings you can specify to change its behavior at run time as discussed in the following sections.

Data Binding mmDataGrid

In the Properties Window, select the BindingSource property. Click the ellipses button [...] next to the property which launches the Binding Source Selection dialog. Select the business object you want to bind the DataGrid to from the list and click OK. For example:

This stores the business object name in the DataGrid's BindingSource property.

If you don't specify any other binding properties, the DataGrid is bound to the business object's default DataTable (specified in the business object's TableName) property. If you want to bind the DataGrid to a different table, specify the table name in the DataGrid's BindingSourceMember property. For example, if you want to bind the DataGrid to a table named UserRoles, you would set the BindingSourceMember property as follows:

Alternately, you can bind the DataGrid to a DataView associated with a given table. To do this, specify the name of the table and data view (separated by a period) in the DataGrid's BindingSourceMember property. For example, if you want to bind the DataGrid to a DataView named dvUserRoles associated with the Roles table, you would set the BindingSourceMember property as follows:

Setting up Columns in mmDataGrid

You can use the standard Visual Studio builders to set up columns in mmDataGrid. The following instructions outline the steps you must take to do this.

  1. In the Visual Studio Properties Window, select the TableStyles property and click its associated ellipses button. This launches the DataGridTableStyle Collection Editor dialog.

  2. Click the Add button to add a new table style to the DataGrid:

  3. In the Properties panel on the right of the dialog, change the (Name) property to a name that describes the table you are binding the DataGrid to. For example, RolesTableStyle.

  4. Set the MappingName property to the name of the DataTable you want to bind the DataGrid to. This is usually the same as the associated business object's TableName property. For example, if you are binding the DataGrid to a business object named Role, and the business object's TableName property is set to "Roles", you would set the MappingName property to Roles. However, if you have specified a different table name to bind to in the DataGrid's BindingSourceMember property, set the MappingName property to this table instead.



    Note: in Visual Studio, you must manually enter the MappingName rather than selecting it from the drop down list. This is because Visual Studio assumes more of a two-tier model where you bind user interface controls directly to data without a middle tier in between.

  5. Select the GridColumnStyles property and click its associated ellipses button. This launches the DataGridColumnStyle Collection Editor dialog.

  6. Click the Add button to add a new column to the DataGrid. This adds a new column to the DataGrid.

  7. Change the Name of the column to something descriptive of its contents. Also set the HeaderText property to the text you want to display for the column at run time.

  8. Set the MappingName property to the name of the field in the DataTable you want to bind the column to at run time.

  9. You can also set other properties of the column such as Alignment, NullText, Width, Format, and so on. Check out the Visual Studio Help for information on setting these properties.

  10. Click OK to close the DataGridColumnStyle dialog, and then click OK again to close the DataGridTableStyle dialog.


Turning Off Automatic New Rows with the Down Arrow Key

By default, when you press the down arrow key when positioned in the last row of a DataGrid, a new row is automatically added to the DataGrid. Although at first this might seem like a nice convenience feature, there are often times when you want to populate a new row with default values or do other types of processing. Adding a new row automatically via the DataGrid completely bypasses the business object where most of this special logic resides.

If you'd like to turn off this automatic behavior, select the mmDataGrid in design mode and go the Properties Window. Select the AddRowsWithDownArrow property and set it to false.

Hooking into the RowChanged Event

Out of the box, the .NET Windows Forms Data Grid doesn't have a RowChanged event, but we've added one to the mmDataGrid class. You can create an event handler for this method and the code will execute whenever the row is changed (either programmatically or by the end user).

Hooking into the SelectedRowCountChanged Event

We have added a SelectedRowCountChanged event to mmDataGrid which is raised whenever the number of selected rows changes. You can create a handler that responds to this event. For example, the following code enables/disables an OK button depending on whether there are any rows selected.

In C#:

private void grdPickList_SelectedRowCountChanged(object sender, OakLeaf.MM.Main.Windows.Forms.mmSelectedRowCountChangeEventArgs e)
{
	this.btnOK.Enabled = e.SelectedRowCount > 0;
}

And in VB .NET:

Private Sub grdPickList_SelectedRowCountChanged(sender As Object, e As OakLeaf.MM.Main.Windows.Forms.mmSelectedRowCountChangeEventArgs) Handles grdPickList.SelectedRowCountChanged
   Me.btnOK.Enabled = e.SelectedRowCount > 0
End Sub


Note: If you want to plug into this event and you have code that programmatically selects/unselects rows in the DataGrid, use the custom MM .NET SelectRow() and UnSelectRow() methods rather than the Select() and UnSelect() methods.

Retrieving Current Row Information

The mmDataGrid class has the following methods that allow you to retrieve information about the currently selected row in the DataGrid.

  • GetCurrentRow - Returns the current row in the grid, as well as the associated business object as an output parameter.

  • GetCurrentRowPK - Returns the primary key value of the currently selected row. Used with data sources that have a single primary key field.

  • GetCurrentRowPKs - Returns the primary key values of the currently selected row. Used with data sources that have multiple primary key fields.

Getting All Selected Rows

We have added a GetSelectedRows method to mmDataGrid that returns a DataSet containing all currently selected rows.

For example, in C#:

DataSet dsSelectedRows = this.grdOrders.GetSelectedRows();

And in VB .NET:

Dim dsSelectedRows As DataSet = Me.grdOrders.GetSelectedRows()

Multi-Select and Single-Select Modes

We have added a custom MultiSelect property to mmDataGrid that allows you to specify if you want users to be able to select multiple rows (the default). If you set this property to false, users can only select one row.

Highlighting an Entire Row When a Cell is Clicked

Normally, when you click a cell in a DataGrid, the individual cell is highlighted:

We have added a SelectRowOnCellClick property to mmDataGrid that, when set to true, highlights the entire row when a cell is clicked:

DataGrid Column Resizing

mmDataGrid has an AutoResizeColumns property that automatically resizes columns proportionately to fill the DataGrid. If you set the ResizeColumn to a specific column in the DataGrid, all other columns will stay their original size and only the column you specify will be resized. For example:


© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 04/27/18
Comment or report problem with topic