Working with the mmDataGridView

The MM .NET Windows Forms mmDataGridView class is a direct subclass of the DataGridView control which is new in .NET 2.0 Windows Forms. Here are some commonly used properties and events:

Member Description
AllowUserToAddRows Specifies if users can add new rows by pressing the down arrow. Set to false by default in mmDataGridView so the business object is not bypassed when adding new rows
AllowUserToDeleteRows    Specifies if users can delete rows by pressing the delete key. Set to false by default in mmDataGridView so the business object is not bypassed when deleting rows.
AutoSizeColumnsMode Specifies the auto size mode for visible columns. Can be set to:
  • AllCells - Adjust to fit the contents of all cells in the columns, including header cells
  • AllCellsExceptHeader - Adjust to fit the contents of all cells in the columns, excluding header cells
  • ColumnHeader - Adjust to fit the contents of the column header cells
  • DisplayedCells - Adjust to fit the contents of all cells in the columns that are in rows currently displayed onscreen, including header cells
  • DisplayedCellsExceptHeader - Adjust to fit the contents of all cells in the columns that are in rows currently displayed onscreen, excluding header cells
  • Fill - Adjust so that the widths of all columns exactly fill the display area of the control, requiring horizontal scrolling only to keep column widths above the DataGridViewColumn.MinimumWidth property values. Relative column widths are determined by the relative DataGridViewColumn.FillWeight property values
  • None - The column widths do not automatically adjust
BackgroundColor Normally you should set this to White. By default, the background color of the DataGridView is set to AppWorkSpace (usually dark grey). If your grid is only partially filled with rows, the dark grey is displayed in the blank area and doesn't look very professional. You can set the background color to White by selecting the BackgroundColor's Custom tab and selecting white from the color palette.
MultiSelect Specifies if the user can select multiple rows
RowHeadersVisible Specifies if row headers (a rectangular selection box at the left of each row) are visible
SelectRows Contains the rows selected by the user
SelectionMode Specifies how the cells of the DataGridView are selected. Possible values are:
  • CellSelect - One or more individual cells can be selected
  • ColumnHeaderSelect - The column will be selected by clicking in the column's header cell. An individual cell can be selected by clicking that cell
  • FullColumnSelect - The entire column will be selected by clicking the column's header or a cell contained in that column
  • FullRowSelect - The entire row will be selected by clicking its row's header or a cell contained in that row
  • RowHeaderSelect - The row will be selected by clicking in the row's header cell. An individual cell can be selected by clicking that cell
CurrentRow Returns the current row in the DataGrid. Check out the section below that desribes custom MM .NET methods GetCurrentRow(), GetCurrentRowPK() and GetCurrentRowPKs() which give more information about the current row
SelectionChanged event Occurs when the current selection changes
RowEnter event Fires when a row is changed and replaces the mmDataGrid.RowChanged event

Data Binding mmDataGridView

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 mmDataGridView to from the list and click OK. For example:

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

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

Alternately, you can bind the mmDataGridView 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 mmDataGridView's BindingSourceMember property. For example, if you want to bind the mmDataGridView to a DataView named dvUserRoles associated with the Roles table, you would set the BindingSourceMember property as follows:

Setting up Columns in mmDataGridView

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

  1. In design mode, right-click the DataGridView and select Edit Columns from the shortcut menu (alternately you can click the DataGridView's Smart Tag and select Edit Columns).

  2. Click the Add... button to add a new column to the DataGridView. This launches the Add Column dialog:

  3. In the Name text box, change the name of the column to something descriptive of its contents. In the Type combo box, select the type of control you want to add to the new column, and in the Header text box enter the text you want to display for the column at run time.

  4. You can also set other properties of the column such as Visible, Read Only, and Frozen. Check out the Visual Studio Help for information on setting these properties.

  5. Click Add to close the Add Column dialog and add the new colum to the DataGridView.

    In the Edit Columns dialog, change the DataPropertyName property to the name of the data column you want to bind to at run time:

  6. When you have finished adding columns to the DataGridView, click OK to close the Edit Columns dialog.

  7. To specify that only the columns you have specified are displayed at run time, you must set the DataGridView's AutoGenerateColumns property to false. Unfortunately, this property is not visible in the property sheet! You have to add code to your form's constructor to set this property to false.

    For example, in C#:

    public MyForm()
    
    	InitializeComponent();
    	this.grdMappings.AutoGenerateColumns = false;
    
    }

    And in VB .NET:

    Public Sub New()
    
    	InitializeComponent()
    	Me.grdMappings.AutoGenerateColumns = False
    
    End Sub

Adding Combo Box Columns to the DataGridView


Note: For a live example of a combo box column in a DataGridView, see the MM .NET Windows Forms sample application Customer Orders Form Order Details DataGridView.

Follow these steps to add a ComboBox column to a DataGridView

  1. Right-click your DataGridView in design mode and select Edit Columns from the shortcut menu.

  2. In the Edit Columns dialog, click the Add button which launches the Add Column dialog. In the Name textbox, specify the name of the column, in the Type column, select DataGridViewComboBoxColumn, specify the Header Text, then click Add, then Close. For example:

  3. In the Edit Columns dialog, set the DataPropertyName property to the column in the DataGridView's data source that sets the currently selected item in the combo box. Click OK to close the dialog.

  4. In the form's constructor, add code that retrieves the list of items to be displayed in the combo box and binds the data to the combo box's DataSource, DisplayMember, and ValueMember properties. Create a form-level variable to hold the DataTable or DataSet used as the data source for the combo box. For example:

    protected DataTable dtProducts;
    protected DataGridViewComboBoxColumn ProductColumn; // auto-generated by the WinForms designer
    
    /// <summary>
    /// Constructor
    /// </summary>
    public CustomerOrdersForm()
    {
    	/// Instantiate and register business objects
    	this.oProduct = (Product)this.RegisterBizObj(new Product());
    
    	//
    	// Required for Windows Form Designer support
    	//
    	InitializeComponent();
    
    	dtProducts = this.oProduct.GetAllData();
    
    	this.ProductColumn.DataSource = dtProducts;
    	this.ProductColumn.DisplayMember = "ProductName";
    	this.ProductColumn.ValueMember = "ProductID";
    }


When your DataGridView displays at run time, the combo box column should be populated with items from your combo box data source and the currently selected item should be set based on the value of the underlying DataGridView data source.

Specifying the DataGridView as the Form's Primary Navigation Control

To specify the DataGridView as your form's primary navigation control set the form's NavControl property accordingly in the form's constructor.

For example, in C#:

this.NavControl = this.grdRoles;

And in VB .NET:

Me.NavControl = Me.grdRoles

Making mmDataGridView Read Only

To make mmDataGridView read-only, set the following properties:

AutoSetEnabled = False
ReadOnly = True

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.

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