C# GridView: add LinkButton and focus selected row
There are some tricks with GridView control on Asp.net. It's really cool tool if you know it. The tricks are about how to add LinkButton to each row and how to focus the GridViewRow where LinkButton was clicked.
You can add custom LinkButton to each row of GridView control in .aspx page this way:
<asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="selectRecord" runat="server" Text="Show" CommandName="Select" CommandArgument='<%# Eval("ID") %>' /> </ItemTemplate> </asp:TemplateField>
If you want to focus the row on RowCommand event, you should use the code like this:
GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); int rowIndex = gvr.RowIndex; resultsGV.Rows[rowIndex].Focus();