简体   繁体   中英

ASP.NET how to add to data to gridview from button click

I have a nested gridview. On the top-level grid, I have a button that should call a stored procedure and and put that data into the nested gridview.

Here is my ASPX page code

<form id="form1" runat="server">
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" CssClass="Grid"
    DataKeyNames="ID" OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button Text="Expand" runat="server" OnClick="Select" CommandArgument='<%#Eval("ID") %>' />
                <asp:Panel ID="pnlOrders" runat="server" Style="display: none">
                    <asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass = "ChildGrid">
                        <Columns>
                            <asp:BoundField ItemStyle-Width="135px" DataField="Label" HeaderText="Label" />
                            <asp:BoundField ItemStyle-Width="150px" DataField="IDs" HeaderText="IDs" />

                            <asp:BoundField ItemStyle-Width="150px" DataFormatString="${0:###,###,###.00}" DataField="Total" HeaderText=Total" />
                        </Columns>
                    </asp:GridView>
                </asp:Panel>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField ItemStyle-Width="150px" DataField="Source" HeaderText="Source" />
        <asp:BoundField ItemStyle-Width="150px" DataField="Label" HeaderText="Label" />
        <asp:BoundField ItemStyle-Width="150px" DataFormatString="${0:###,###,###.00}" DataField="Total20" HeaderText="Total20" />
        <asp:BoundField ItemStyle-Width="150px" DataFormatString="${0:###,###,###.00}" DataField="Total1" HeaderText="Total1" />
        <asp:BoundField ItemStyle-Width="150px" DataFormatString="{0:P1}"  DataField="Average_Fee_PCT" HeaderText="Average Fee" />
    </Columns>
</asp:GridView>
</form>
        

Here is my C# code

protected void Select(object sender, EventArgs e)
{
    string ID = (sender as Button).CommandArgument;

            Response.Write("<script>console.log('ID...." + ID + "');</script>");
            
        GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;          
        gvOrders.DataSource = GetData(string.Format("exec ReturnDataSingle '" + ID + "', '"+ DateTime.Now.ToString("yyyy-MM-dd") + "'"));
        gvOrders.DataBind();


}

Here is the error message I am getting

'System.EventArgs' does not contain a definition for 'Row' and no extension method 'Row' accepting a first argument of type 'System.EventArgs' could be found

Can someone help me figure out how I would be able to add the stored procedure data to the gvOrders gridview based off that button click?

Try to convert the sender object into the button that was clicked, followed by this, get the NamingContainer of the button and in the row ( GridViewRow ) look for the GridView control, the next part is to load the data:

protected void Select(object sender, EventArgs e)
{
    Button btn= (Button)sender;

    GridViewRow row = (GridViewRow)btn.NamingContainer;

    GridView GridChild = ((GridView)row.FindControl("gvOrders"));

    // Fill GridView
    GridChild.DataSource = GetData(string.Format("exec ReturnDataSingle '" + ID + "', '"+ DateTime.Now.ToString("yyyy-MM-dd") + "'"));
    GridChild.DataBind();
}

The problem was that the Row property is applicable on a GridViewRowEventArgs object, normally it is handled in the RowDataBound event but you need to fire the event through Click, therefore you are forced to use EventArgs object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM