I am using a buttonfield to perform updating of database. However, I couldn't get the row data from GridView despite searching for multiple solutions.
I tried something like like :
GridViewRow gvRow = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
Label lbitemid = (Label)gvRow.FindControl("lblitemid");
but was not successful either. I got this error : System.InvalidCastException: 'Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.Button'.'
<asp:GridView ID="QgridView" AutoGenerateColumns="False"
CssClass="table table-bordered" AllowPaging="True" PageSize="6"
BackColor="White" BorderColor="Black" BorderStyle="Solid"
ForeColor="Black" GridLines="None" runat="server"
OnRowCommand="QgridView_RowCommand"
OnSelectedIndexChanged="QgridView_SelectedIndexChanged" >
<Columns>
<asp:TemplateField HeaderText="No">
<ItemTemplate>
<span>
<%#Container.DataItemIndex + 1%>
</span>
</ItemTemplate>
</asp:TemplateField>
<asp:ImageField HeaderText="Image" DataImageUrlField="coverimg" >
<ControlStyle CssClass="coverimage"/>
<ItemStyle HorizontalAlign="Center" />
</asp:ImageField>
<asp:BoundField HeaderText="Buyer" DataField="buyer" />
<asp:TemplateField HeaderText="Item">
<EditItemTemplate>
<asp:TextBox ID="tbbuyer" runat="server" Text='<%# Bind("item") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Lblbuyer" runat="server" Text='<%# Bind("item") %>'>
</asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Price offered" DataField="price" />
<asp:buttonfield buttontype="Button" commandname="Accept"
text="Accept"/>
<asp:TemplateField HeaderText="quoteid">
<EditItemTemplate>
<asp:TextBox ID="tbquoteid" runat="server" Text='<%#
Bind("quoteid") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblquoteid" runat="server" Text='<%#
Bind("quoteid") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="hiddencol" />
<ItemStyle CssClass="hiddencol" />
</asp:TemplateField>
<asp:TemplateField HeaderText="id">
<EditItemTemplate>
<asp:TextBox ID="tbitemid" runat="server" Text='<%# Bind("id")
%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblitemid" runat="server" Text='<%# Bind("id")
%>'>
</asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="hiddencol" />
<ItemStyle CssClass="hiddencol" />
</asp:TemplateField>
<asp:TemplateField HeaderText="rtype">
<EditItemTemplate>
<asp:TextBox ID="tbtype" runat="server" Text='<%# Bind("rtype")
%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbltype" runat="server" Text='<%# Bind("rtype")
%>'></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="hiddencol" />
<ItemStyle CssClass="hiddencol" />
</asp:TemplateField>
<asp:TemplateField HeaderText="seller">
<EditItemTemplate>
<asp:TextBox ID="tbseller" runat="server" Text='<%#
Bind("seller") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblseller" runat="server" Text='<%# Bind("seller")
%>'></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="hiddencol" />
<ItemStyle CssClass="hiddencol" />
</asp:TemplateField>
<asp:buttonfield buttontype="Button"
commandname="View"
text="View"/>
</Columns>
</asp:GridView>
//Code behind
protected void QgridView_RowCommand(object sender,
GridViewCommandEventArgs e)
{
System.Diagnostics.Debug.WriteLine("onrowcommand");
if (e.CommandName == "View")
{
System.Diagnostics.Debug.WriteLine("ButtonView is clicked");
Response.Redirect("ListingItems.aspx");
}
else if (e.CommandName == "Accept")
{
System.Diagnostics.Debug.WriteLine("buttonAccept is
clicked");
productDAO productdao = new productDAO();
//GridViewRow row = QgridView.SelectedRow;
//Get Row data
GridViewRow gvRow
(GridViewRow(((Button)e.CommandSource).NamingContainer);
Label lbitemid = (Label)gvRow.FindControl("lblitemid");
int id = Convert.ToInt32(lbitemid.Text); //Error returned
//int quoteid = Convert.ToInt32(row.Cells[7].Text);
System.Diagnostics.Debug.WriteLine("itemid = ", id);
//productdao.GridPush(quoteid, id);
}
else
{
System.Diagnostics.Debug.WriteLine("no command name");
}
}
How can I get the data for each row in my RowCommand?