Export (0) Print
Expand All
Expand Minimize
1 out of 5 rated this helpful - Rate this topic

DataGridView.MultiSelect Property

Note: This property is new in the .NET Framework version 2.0.

Gets or sets a value indicating whether the user is allowed to select more than one cell, row, or column of the DataGridView at a time.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public bool MultiSelect { get; set; }
/** @property */
public boolean get_MultiSelect ()

/** @property */
public void set_MultiSelect (boolean value)

public function get MultiSelect () : boolean

public function set MultiSelect (value : boolean)

Property Value

true if the user can select more than one cell, row, or column at a time; otherwise, false. The default is true.

When the MultiSelect property is set to true, multiple elements (cells, rows, or columns) can be selected in the DataGridView control. To select multiple elements, the user can hold down the CTRL key while clicking the elements to select. Consecutive elements can be selected by clicking the first element to select and then, while holding down the SHIFT key, clicking the last element to select. The selection scope is based on the SelectionMode property. For example, if SelectionMode is set to DataGridViewSelectionMode.FullColumnSelect, the user can select multiple columns.

You can use the MultiSelect property to allow a user to select multiple elements in the DataGridView control and perform an operation on all the selected elements. For example, the user could select multiple cells and then right-click a selected cell to display a shortcut menu that displays a set of tasks to perform on the selected cells.

To determine which cells, rows, or columns are selected in the DataGridView control, you can access the SelectedCells, SelectedRows, or SelectedColumns property. To determine the number of selected cells, call the GetCellCount method with an argument value of DataGridViewElementStates.Selected. Use the GetRowCount method to retrieve the number of selected rows and the GetColumnCount method to retrieve the number of columns. These methods are more efficient than accessing the collections directly when working with large amounts of data. For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

The following code example demonstrates how to use the MultiSelect property. To run this example, paste the code into a form that contains a DataGridView named dataGridView1, and then call the SetUpDataGridView method from the form's constructor or Load event handler. Ensure all events are associated with their event handlers.

private void SetUpDataGridView()
{
    this.Controls.Add(dataGridView1);
    dataGridView1.ColumnCount = 5;
    DataGridViewCellStyle style = 
        dataGridView1.ColumnHeadersDefaultCellStyle;
    style.BackColor = Color.Navy;
    style.ForeColor = Color.White;
    style.Font = new Font(dataGridView1.Font, FontStyle.Bold);

    dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
    dataGridView1.Name = "dataGridView1";
    dataGridView1.Location = new Point(8, 8);
    dataGridView1.Size = new Size(500, 300);
    dataGridView1.AutoSizeRowsMode = 
        DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
    dataGridView1.ColumnHeadersBorderStyle = 
        DataGridViewHeaderBorderStyle.Raised;
    dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Single;
    dataGridView1.GridColor = SystemColors.ActiveBorder;
    dataGridView1.RowHeadersVisible = false;

    dataGridView1.Columns[0].Name = "Release Date";
    dataGridView1.Columns[1].Name = "Track";
    dataGridView1.Columns[1].DefaultCellStyle.Alignment = 
        DataGridViewContentAlignment.MiddleCenter;
    dataGridView1.Columns[2].Name = "Title";
    dataGridView1.Columns[3].Name = "Artist";
    dataGridView1.Columns[4].Name = "Album";

    // Make the font italic for row four.
    dataGridView1.Columns[4].DefaultCellStyle.Font = new Font(DataGridView.DefaultFont, FontStyle.Italic);

    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    dataGridView1.MultiSelect = false;

    dataGridView1.BackgroundColor = Color.Honeydew;

    dataGridView1.Dock = DockStyle.Fill;

    dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
    dataGridView1.CellParsing += new DataGridViewCellParsingEventHandler(dataGridView1_CellParsing);
    addNewRowButton.Click += new EventHandler(addNewRowButton_Click);
    deleteRowButton.Click += new EventHandler(deleteRowButton_Click);
    ledgerStyleButton.Click += new EventHandler(ledgerStyleButton_Click);
    dataGridView1.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);

}

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.