Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select a complete dataGridView Row when the user clicks a cell of that row?

People also ask

How to get selected row DataGridView?

To get the selected rows in a DataGridView controlUse the SelectedRows property. To enable users to select rows, you must set the SelectionMode property to FullRowSelect or RowHeaderSelect.

How do you select a full row of a table through the SelectionMode property?

Default selection mode. Clicking a cell selects it. Clicking a row header selects the entire row.


You need to set datagridview's SelectionMode to FullRowMode.

Note: In Visual Studio 2013 with .NET 4.5 the property is called FullRowSelect.


If you want the row selected programatically, you would use the datagridview's cell click event: shown in VB.net and C#

VB.Net

Private Sub dgvGrid_CellClick(sender as System.Object, e as System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvGrid.CellClick
    If e.RowIndex < 0 Then
        Exit Sub
    End If

    intIndex = e.RowIndex
    dgvGrid.Rows(intIndex).Selected = True
Exit Sub

C#

private void dgvRptTables_CellClick(System.Object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
    if (e.RowIndex < 0) {
        return;
    }

    int index = e.RowIndex;
    dgvGrid.Rows[index].Selected = true;
}

In the DataGridView properties, Set

  • MultiSelect -> True
  • SelectionMode -> FullRowSelect


Could do something like this

protected override void Render(HtmlTextWriter writer)
{
    foreach (GridViewRow row in Results.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            row.Attributes["onmouseover"] = "this.style.cursor='pointer';";
            row.CssClass = "rowHover";
            row.ToolTip = "Click row to view person's history";
            row.Attributes.Add("onclick", this.ClientScript.GetPostBackClientHyperlink(this.Results,"Select$" & r.RowIndex , true));
        }
    }

    base.Render(writer);
}