Ok, you look to have a nested set of GridViews. However, before we delve deeper here? As a general rule, you don't have to use the built in GridView events, and in fact most cases the efforts to do so are not worth your time. In other words, just "ignore" the command name options and event model of the GridView. Just drop in a plane Jane regular button or even a link button. (no real need to use a link button, the regular button can be used). The "magic trick" then becomes how do you wire up a click event for these nested controls inside of the GridView? You can't double click on the button, so the approach is to THEN simply enter the event in the markup. Say this simple GridView with a link button (but, we can use a regular button if we wish here. In fact, most controls, even a check box, or combo box can be used and to setup the controls events, then just type in the event and "=" then then hit ctrl-space. That brings up the "magic" context menu, and thus you can now add a simple regular click event to the button, or link button.
So, our markup looks like this:
<asp:GridView ID="GVHotels" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table table-hover"
width="40%">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="HotelName" HeaderText="HotelName" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField HeaderText="View"
ItemStyle-HorizontalAlign="Center"
HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="120px" >
<ItemTemplate>
<asp:LinkButton ID="cmdView" runat="server"
CssClass="btn myshadow"
>
<span aria-hidden="true" class="glyphicon glyphicon-file"></span>View
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="GridPager" />
</asp:GridView>
The process looks like this:
So, now with above, we are free to write a simple regular click event. The next part is how to get the current row click in that event? The code looks like this:
protected void cmdView_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
GridViewRow gRow = (GridViewRow)btn.NamingContainer;
int PK = (int)GVHotels.DataKeys[gRow.RowIndex]["ID"];
string sInfo = $@"Database PK = {PK} <br/>
Row index click = {gRow.RowIndex}<br/>
Hotel Name = {gRow.Cells[3].Text}";
LabelClickInfo.Text = sInfo;
}
And our code to load the GridView looks like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadGrid();
}
void LoadGrid()
{
string strSQL = @"SELECT * FROM tblhotelsA
ORDER BY HotelName";
GVHotels.DataSource = General.MyRst(strSQL);
GVHotels.DataBind();
}
And the end result looks like this:
So note how we don't bother with the GridView "command" stuff at all. We ignore all that, drop in a button, or link button. Add a standard "regular" click event as per above. Then we use the "trick" of the "naming container" which allows you to pick up the current row click.