Thursday, August 1, 2013

Get gridview selected row

There was this requirement in one of my projects that it will display a confirm dialog box whenever the user would click on the select button. Thanks to the Ajax Control Toolkit (you can download the latest version here), I was able to use the Confirm Button control. By doing so, I would need to convert the select button column in my grid view to a template column (thus converting the button to a link button in my grid view).

While expecting that I would still get the selected row (using the command argument), I found out that this is does not happen.

Why?

Because while we did convert it successfully, the link button does not tie up easily to my selected row [Note: I will update this explanation once I've confirmed it fully. But for now, this is what really happens]. Initially, I would add in a custom attribute on the row inside the GridView.RowCreated event. However, I found a better solution and I did this by tweaking in the CommandArgument property.
 <asp:PagingGridView ID="UsersGridView" runat="server"   
           CellPadding="4" ForeColor="#333333" GridLines="None"  
           DataKeyNames="ID"   
           AutoGenerateColumns="False" AllowSorting="True"   
           ShowHeaderWhenEmpty="True"   
           OnPageIndexChanging="UsersGridView_PageIndexChanging"   
           OnRowCommand="UsersGridView_RowCommand"   
           OnRowEditing="UsersGridView_RowEditing" AllowPaging="True">  
   <AlternatingRowStyle BackColor="White" />  
   <Columns>  
     <asp:TemplateField ShowHeader="False">  
       <ItemTemplate>  
         <asp:LinkButton ID="LinkButton1"   
           runat="server"   
           CausesValidation="false"   
           CommandName="Delete"   
           CommandArgument="<%# Container.DataItemIndex %>"  
           Text="Delete"/>  
         <asp:ConfirmButtonExtender ID="LinkButton1_ConfirmButtonExtender"   
           runat="server"   
           ConfirmText="Are you sure you want to delete this record?"   
           Enabled="True"   
           TargetControlID="LinkButton1"/>  
       </ItemTemplate>  
     </asp:TemplateField>  
     <asp:BoundField DataField="ID" HeaderText="ID" />  
     <asp:BoundField DataField="Name" HeaderText="Name" />  
     <asp:BoundField DataField="Address" HeaderText="Address" />  
     <asp:BoundField DataField="Group" HeaderText="User Group" />  
     <asp:BoundField DataField="IsActive" HeaderText="Is Active" />  
   </Columns>  
   <EditRowStyle BackColor="#7C6F57" />  
   <EmptyDataTemplate>  
     No records found  
   </EmptyDataTemplate>  
   <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />  
   <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />  
   <PagerSettings Mode="NumericFirstLast"/>  
   <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />  
   <RowStyle BackColor="#E3EAEB" />  
   <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />  
   <SortedAscendingCellStyle BackColor="#F8FAFA" />  
   <SortedAscendingHeaderStyle BackColor="#246B61" />  
   <SortedDescendingCellStyle BackColor="#D4DFE1" />  
   <SortedDescendingHeaderStyle BackColor="#15524A" />  
 </asp:PagingGridView>  
By adding that section in the CommandArgument property, we are now able to get the selected row through the CommandArgument property.

Hope this helps!

Happy coding!

No comments:

Post a Comment