DataGridView.MultiSelect 屬性

定義

取得或設定值,指出是否允許使用者同時選取一個以上的 DataGridView 儲存格、資料列或資料行。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.

public:
 property bool MultiSelect { bool get(); void set(bool value); };
public bool MultiSelect { get; set; }
member this.MultiSelect : bool with get, set
Public Property MultiSelect As Boolean

屬性值

Boolean

如果使用者可以同時選取一個以上的儲存格、資料列或資料行,則為true ;否則為 falsetrue if the user can select more than one cell, row, or column at a time; otherwise, false. 預設為 trueThe default is true.

範例

下列程式碼範例示範如何使用 MultiSelect 屬性。The following code example demonstrates how to use the MultiSelect property. 若要執行這個範例,請將程式碼貼到包含名為的表單 DataGridView dataGridView1 中,然後 SetUpDataGridView 從表單的函式或 Load 事件處理常式呼叫方法。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.

void SetUpDataGridView()
{
   this->Controls->Add( dataGridView1 );
   dataGridView1->ColumnCount = 5;
   DataGridViewCellStyle^ style = dataGridView1->ColumnHeadersDefaultCellStyle;
   style->BackColor = Color::Navy;
   style->ForeColor = Color::White;
   style->Font = gcnew System::Drawing::Font( dataGridView1->Font,FontStyle::Bold );
   dataGridView1->EditMode = DataGridViewEditMode::EditOnEnter;
   dataGridView1->Name = "dataGridView1";
   dataGridView1->Location = Point(8,8);
   dataGridView1->Size = System::Drawing::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 = gcnew System::Drawing::Font( DataGridView::DefaultFont,FontStyle::Italic );
   dataGridView1->SelectionMode = DataGridViewSelectionMode::FullRowSelect;
   dataGridView1->MultiSelect = false;
   dataGridView1->BackgroundColor = Color::Honeydew;
   dataGridView1->Dock = DockStyle::Fill;
   dataGridView1->CellFormatting += gcnew DataGridViewCellFormattingEventHandler( this, &Form1::dataGridView1_CellFormatting );
   dataGridView1->CellParsing += gcnew DataGridViewCellParsingEventHandler( this, &Form1::dataGridView1_CellParsing );
   addNewRowButton->Click += gcnew EventHandler( this, &Form1::addNewRowButton_Click );
   deleteRowButton->Click += gcnew EventHandler( this, &Form1::deleteRowButton_Click );
   ledgerStyleButton->Click += gcnew EventHandler( this, &Form1::ledgerStyleButton_Click );
   dataGridView1->CellValidating += gcnew DataGridViewCellValidatingEventHandler( this, &Form1::dataGridView1_CellValidating );
}
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);
}
Private Sub SetUpDataGridView()

    Me.Controls.Add(dataGridView1)
    dataGridView1.ColumnCount = 5

    With dataGridView1.ColumnHeadersDefaultCellStyle
        .BackColor = Color.Navy
        .ForeColor = Color.White
        .Font = New Font(dataGridView1.Font, FontStyle.Bold)
    End With

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

        .Columns(0).Name = "Release Date"
        .Columns(1).Name = "Track"
        .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        .Columns(2).Name = "Title"
        .Columns(3).Name = "Artist"
        .Columns(4).Name = "Album"

        ' Make the font italic for row four.
        .Columns(4).DefaultCellStyle.Font = _
            New Font(Control.DefaultFont, _
                FontStyle.Italic)

        .SelectionMode = _
            DataGridViewSelectionMode.FullRowSelect
        .MultiSelect = False

        .BackgroundColor = Color.Honeydew

        .Dock = DockStyle.Fill
    End With

End Sub

備註

MultiSelect 屬性設定為時 true ,可以在控制項中選取多個元素 (資料格、資料列或資料行) DataGridViewWhen the MultiSelect property is set to true, multiple elements (cells, rows, or columns) can be selected in the DataGridView control. 若要選取多個元素,使用者可以按住 CTRL 鍵,同時按一下要選取的專案。To select multiple elements, the user can hold down the CTRL key while clicking the elements to select. 您可以按一下第一個要選取的專案,然後按住 SHIFT 鍵,然後按一下最後一個要選取的專案,來選取連續的元素。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. 選取範圍是以屬性為基礎 SelectionModeThe selection scope is based on the SelectionMode property. 例如,如果 SelectionMode 設定為 DataGridViewSelectionMode.FullColumnSelect ,則使用者可以選取多個資料行。For example, if SelectionMode is set to DataGridViewSelectionMode.FullColumnSelect, the user can select multiple columns.

您可以使用 MultiSelect 屬性,讓使用者選取控制項中的多個元素 DataGridView ,並對所有選取的專案執行作業。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.

若要判斷在控制項中選取哪些資料格、資料列或資料行, DataGridView 您可以存取 SelectedCellsSelectedRowsSelectedColumns 屬性。To determine which cells, rows, or columns are selected in the DataGridView control, you can access the SelectedCells, SelectedRows, or SelectedColumns property. 若要判斷選取的儲存格數目,請 GetCellCount 使用的引數值來呼叫方法 DataGridViewElementStates.SelectedTo determine the number of selected cells, call the GetCellCount method with an argument value of DataGridViewElementStates.Selected. GetRowCount 可以使用方法來抓取選取的資料列數目,以及取得資料行 GetColumnCount 數目的方法。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. 如需詳細資訊,請參閱 縮放 Windows Form DataGridView 控制項的最佳做法For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

適用於