DataGridView.MultiSelect 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置一个值,该值指示是否允许用户一次选择 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名为 dataGridView1
的窗体中,然后从窗体的构造函数或Load事件处理程序调用 SetUpDataGridView
方法。 确保所有事件都与其事件处理程序相关联。
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 键的同时单击要选择的最后一个元素来选择连续元素。 选择范围基于 SelectionMode 属性。 例如,如果 SelectionMode 设置为 DataGridViewSelectionMode.FullColumnSelect,则用户可以选择多个列。
可以使用 MultiSelect 属性允许用户选择控件中的 DataGridView 多个元素,并对所有选定的元素执行操作。 例如,用户可以选择多个单元格,然后右键单击所选单元格以显示快捷菜单,该快捷菜单显示要对所选单元格执行的一组任务。
若要确定在 控件中选择 DataGridView 了哪些单元格、行或列,可以访问 SelectedCells、 SelectedRows或 SelectedColumns 属性。 若要确定所选单元格的数目,请使用参数值 DataGridViewElementStates.Selected调用 GetCellCount 方法。 GetRowCount使用 方法检索所选行数,GetColumnCount使用 方法检索列数。 在处理大量数据时,这些方法比直接访问集合更有效。 有关详细信息,请参阅 缩放 Windows 窗体 DataGridView 控件的最佳做法。