asp:GridView Row Selection Ideas

GridViews are nice for appearing tabular, repeated information. As with all tabular information, occasionally your dataset has too many fields to turn throughout the desk. Therefore, you wish to have to make the row “clickable” so you’ll be able to display the additional element to the person. You might submit the document ID to a view web page and even popup the document element in a JQuery conversation. There are some ways to turn the element so that you must believe which manner is perfect to your website online navigation. This article discusses how to choose a row on a GridView and do one thing with it. A great situation for this method is a GridView that, when a row is chosen, refreshes a kid GridView and display hierarchical information.

The local row variety manner for an asp:GridView is so as to add a button labelled “Select” originally or finish of the row. Then the person clicks the make a choice button which executes code to turn the element. To make navigation more straightforward for the person, I love to make all of the row “selectable” so the person does not need to click on the button to generate the view. (Note: this could also be an accessibility factor too).

Note the asp:CommandField added to the GridView. This will upload a SELECT button to the GridView, alternatively the Visible=”false” way it’s not proven within the markup. We are not looking for it within the markup, as a result of we can care for the capability within the codebehind. But we can bind the codebehind to the CommandField. To do that, we wish to bind the row OnClick tournament to the GridView (I referred to as my GridView GridView1) row make a choice capability. We do that in code as underneath.

secure void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)


if (e.Row.RowKind == DataControlRowKind.KnowledgeRow)


e.Row.Attributes["onmouseover"] = "this.taste.cursor='hand';this.taste.textDecoration='underline';";

e.Row.Attributes["onmouseout"] = "this.taste.textDecoration='none';";

e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);



So what is going on this is:

  • When the Row information is certain to the GridView
  • Set attributes at the row
  • OnMouseOver – trade the cursor and underline the textual content
  • OnMouseOut – take away the textual content underline
  • OnClick – name the GridView make a choice manner

Each line then has energetic taste adjustments in keeping with mouseover/out occasions and can reply to an OnClick tournament. You can configure this tournament as you could possibly in most cases accomplish that within the asp:CommandField. Alternatively, you might want to bind the OnClick tournament to any other asp:CommandField serve as is the default motion for a row click on must be an replace or insert name.

Til subsequent time.