DataGridView.MultiSelect 속성

정의

사용자가 DataGridView의 셀, 행 또는 열을 한 번에 둘 이상 선택할 수 있는지 여부를 나타내는 값을 가져오거나 설정합니다.

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

속성 값

사용자가 한 번에 둘 이상의 셀, 행 또는 열을 선택할 수 있으면 true이고, 그렇지 않으면 false입니다. 기본값은 true입니다.

예제

다음 코드 예제를 사용 하는 방법에 설명 합니다 MultiSelect 속성입니다. 이 예제를 실행하려면 라는 가 포함된 DataGridView 폼에 코드를 붙여넣은 다음 폼의 생성자 또는 Load 이벤트 처리기에서 메서드를 호출 SetUpDataGridViewdataGridView1합니다. 모든 이벤트가 해당 이벤트 처리기와 연결되어 있는지 확인합니다.

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되면 컨트롤에서 여러 요소(셀, 행 또는 열)를 DataGridView 선택할 수 있습니다. 여러 요소를 선택하려면 사용자가 Ctrl 키를 누른 채 선택할 요소를 클릭할 수 있습니다. 선택할 첫 번째 요소를 클릭한 다음 SHIFT 키를 누른 상태에서 선택할 마지막 요소를 클릭하여 연속 요소를 선택할 수 있습니다. 선택 scope 기반으로 합니다 SelectionMode 속성입니다. 예를 들어 가 로 DataGridViewSelectionMode.FullColumnSelect설정된 경우 SelectionMode 사용자는 여러 열을 선택할 수 있습니다.

사용 하 여는 MultiSelect 속성을 사용자가 컨트롤에서 DataGridView 여러 요소를 선택 하 고 선택한 모든 요소에 대 한 작업을 수행할 수 있도록 합니다. 예를 들어 사용자는 여러 셀을 선택한 다음 선택한 셀을 마우스 오른쪽 단추로 클릭하여 선택한 셀에서 수행할 작업 집합을 표시하는 바로 가기 메뉴를 표시할 수 있습니다.

컨트롤에서 DataGridView 선택한 셀, 행 또는 열을 확인하려면 , SelectedRows또는 SelectedColumns 속성에 SelectedCells액세스할 수 있습니다. 선택한 셀 수를 확인하려면 인수 값DataGridViewElementStates.Selected이 인 GetCellCount 메서드를 호출합니다. 메서드를 GetRowCount 사용하여 선택한 행 수를 검색하고 메서드를 GetColumnCount 사용하여 열 수를 검색합니다. 이러한 메서드는 대량의 데이터로 작업할 때 컬렉션에 직접 액세스하는 것보다 더 효율적입니다. 자세한 내용은 Windows Forms DataGridView 컨트롤의 크기를 조정하는 최선의 방법을 참조하세요.

적용 대상

추가 정보