Selected Property

DataGridViewRow.Selected Property

 

Gets or sets a value indicating whether the row is selected.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public override bool Selected { get; set; }

Property Value

Type: System.Boolean

true if the row is selected; otherwise, false.

Exception Condition
InvalidOperationException

The row is in a DataGridView control and is a shared row.

You cannot use this property with a shared row. For more information about shared rows, see Best Practices for Scaling the Windows Forms DataGridView Control.

The following code example illustrates the use of this property.

void dataGridView1_ColumnHeaderMouseClick(
    object sender, DataGridViewCellMouseEventArgs e)
{
    this.dataGridView1.SelectionMode =
        DataGridViewSelectionMode.ColumnHeaderSelect;
    this.dataGridView1.Columns[e.ColumnIndex].HeaderCell
        .SortGlyphDirection = SortOrder.None;
    this.dataGridView1.Columns[e.ColumnIndex].Selected = true;
}

void dataGridView1_RowHeaderMouseClick(
    object sender, DataGridViewCellMouseEventArgs e)
{
    this.dataGridView1.SelectionMode =
        DataGridViewSelectionMode.RowHeaderSelect;
    this.dataGridView1.Rows[e.RowIndex].Selected = true;
}

.NET Framework
Available since 2.0
Return to top
Show:
© 2016 Microsoft