DataGridView.SelectionMode 属性

定义

获取或设置一个值,该值指示如何选择 DataGridView 的单元格。

C#
[System.ComponentModel.Browsable(true)]
public System.Windows.Forms.DataGridViewSelectionMode SelectionMode { get; set; }

属性值

DataGridViewSelectionMode 值之一。 默认值为 RowHeaderSelect

属性

例外

当设置此属性时,指定的值不是有效的 DataGridViewSelectionMode 值。

当设置此属性时,指定的值为 FullColumnSelectColumnHeaderSelect,并且一个或多个列的 SortMode 属性设置为 Automatic

示例

下面的代码示例演示如何设置 ColumnHeadersDefaultCellStyle、、AutoSizeRowsModeColumnHeadersBorderStyleCellBorderStyleGridColorBackgroundColorRowHeadersVisibleSelectionModeColumnCountMultiSelect 属性。 此外,该代码示例还演示如何设置 DataGridViewColumn.NameDataGridViewColumn.DefaultCellStyle 属性。 若要运行此示例,请将代码粘贴到包含DataGridView名为 dataGridView1的窗体中,然后从窗体的构造函数或Load事件处理程序调用 SetUpDataGridView 方法。 确保所有事件都与其事件处理程序相关联。

C#
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);
}

注解

更改 属性的值将 SelectionMode 清除当前所选内容。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅