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.
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
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With