Select a complete row in a ASP.NET (2.0) gridview

For a project we must be able to select a complete row in a gridview instead of using the Select button.

We found a good solution on the following website http://www.geekzilla.co.uk/View9FC28EE6-ACB0-4F51-BFE4-38B0B10134D5.htm.

And indeed if you try it out like described you will get a enableEventValidation error. The comments are describing how to solve it, but in different versions and solutions.

The solution that we found is that you put a select column (gridview specific column) on the gridview and you change all the CSS styles (ControlStyle, HeaderStyle, FooterStyle, ItemStyle)  of the column to a new style element with display:none.

So here you go, great solution for complete row selection in ASP.NET 2.0 gridview.

- Wesley

To understand why I post this you must read my next post (all credits goes to Paul Marshall):

ASP.Net's GridViews can be quite useful, but beware of binding them to huge datasets as this has an overhead on the ViewState.

Often you'll want to display a number of columns on each line and row space becomes an issue. What's worse is you then have to create a SELECT command button to be able to access that line's data.

<asp:CommandField ShowInsertButton="true" />

Use the following code on the event OnRowDataBound to eliminate the need for the SELECT command field and save yourself some valuable space.

Here is the HTML to create a GridView, I'm displaying a list of people, and the key for each record is the PERSON_ID.

<asp:GridView ID="PeopleGridView" runat="server" 
    AutoGenerateColumns="False" 
    DataKeyNames="PERSON_ID" 
    DataSourceID="PeopleDataObject" 
    Width="200px" 
    OnRowDataBound="PeopleGridView_RowDataBound" 
    AllowPaging="True">
    <Columns>  
        <asp:BoundField 
            DataField="USER_NAME" 
            HeaderText="Name" 
            SortExpression="USER_NAME" >
        </asp:BoundField>
    </Columns>
</asp:GridView>

The key event to note is the OnRowDataBound, use the following code to create SELECT functionality on the row.

protected void PeopleGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";

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

Each row will then behave like a link, and when you select one it can drive the behavior of another control(s) on your page, possibly a DetailsView allowing you to INSERT a complete record to the database.

Published Tuesday, October 02, 2007 7:59 AM by wesleyw

Comments

# re: Select a complete row in a ASP.NET (2.0) gridview

Thursday, June 26, 2008 12:41 AM by sethu

Hey Give me your post

# re: Select a complete row in a ASP.NET (2.0) gridview

Monday, June 30, 2008 8:11 AM by Wesley

Heu? What do you mean?

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above:
Powered by Community Server (Non-Commercial Edition), by Telligent Systems