Created On:  10 February 2012
Problem:
When using a GridView control on an ASP.NET web page, is there a way to allow a user to click on any row in the grid and do some processing on the selected line?  It seems as if you must generate a select button for each row in order to generate the select event.
Resolution:
You can cause a GridView to return the SelectedIndexChanged event by adding the following code to the RowCreated event.
You must also add the following to the Page header in order to avoid a Postback error:
EnableEventValidation="false"
When the row is clicked it will send the Select command to the GridView which will cause the SelectedIndexChanged event to fire. Make sure that you have the SelectIndexChanged event wired up to a method in your program as this will be called when the event is fired and then add the following code:
You must also add the following to the Page header in order to avoid a Postback error:
EnableEventValidation="false"
When the row is clicked it will send the Select command to the GridView which will cause the SelectedIndexChanged event to fire. Make sure that you have the SelectIndexChanged event wired up to a method in your program as this will be called when the event is fired and then add the following code:
method-id GridView1_RowCreated protected.
procedure division using by value sender as object e as type System.Web.UI.WebControls.GridViewRowEventArgs.
       
     if (e::Row::RowType = type DataControlRowType::DataRow)
        set e::Row::Attributes::Item("onmouseover") to "this.style.cursor='pointer';this.style.textDecoration='underline';"
        set e::Row::Attributes::Item("onmouseout") to "this.style.textDecoration='none';"
        set e::Row::Attributes::Item("onclick") to self::ClientScript::GetPostBackClientHyperlink(self::GridView1, "Select$" & e::Row::RowIndex, false)
     end-if    
    
 end method.
Old KB# 35490
        
