DataGridView.RowHeadersVisible 属性

定义

获取或设置一个值,该值指示是否显示包含行标题的列。

C#
public bool RowHeadersVisible { get; set; }

属性值

如果显示包含行标题的列,为true ;否则为 false。 默认值为 true

例外

当设置此属性时,指定的值为 false,并且 AutoSizeRowsMode 属性设置为 AllHeadersDisplayedHeaders

示例

下面的代码示例演示如何设置 ColumnHeadersDefaultCellStyle、、、AutoSizeRowsModeCellBorderStyleColumnHeadersBorderStyleGridColorRowHeadersVisibleBackgroundColor、、 SelectionModeColumnCountMultiSelect 属性。 此外,代码示例还演示如何设置 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);
}

注解

RowHeadersVisible如果 属性为 false,则属性设置为 true 的行Resizable仍可在用户界面 (UI) 中调整大小,方法是单击要调整大小的行底部边框上的任意位置的鼠标光标。

适用于

产品 版本
.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

另请参阅