Monday, April 21, 2008

ASP.NET GridView Tips - Select row onclick

If you want to select a row in GridView by clicking anywhere on the row, here is a solution.

Check 'Enable Selection' from GridView tasks and put the following code in the RowDataBound event of the GridView as below.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then

e.Row.Attributes.Add("onmouseover", "'hand';'underline';")
e.Row.Attributes.Add("onmouseout", "'none';")

e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(Me.GridView1, "Select$" + e.Row.RowIndex.ToString()))
End If
End Sub

If you want to hide the 'Select' link in the rows, simplest way is to clear the 'SelectText' property of 'Select' CommandField (from 'Edit Columns...' task).

No comments: