简体   繁体   English

ASP.NET 如何通过单击按钮将数据添加到 gridview

[英]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.在顶层网格上,我有一个按钮,它应该调用一个存储过程并将该数据放入嵌套的 gridview 中。

Here is my ASPX page code这是我的 ASPX 页面代码

<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这是我的 C# 代码

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 “System.EventArgs”不包含“Row”的定义,并且找不到接受“System.EventArgs”类型的第一个参数的扩展方法“Row”

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?有人可以帮我弄清楚如何根据该按钮单击将存储过程数据添加到 gvOrders gridview 吗?

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:尝试将sender对象转换为被点击的按钮,然后,获取按钮的NamingContainer并在行( GridViewRow )中查找 GridView 控件,接下来是加载数据:

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问题是Row属性适用于GridViewRowEventArgs对象,通常在RowDataBound事件中处理,但您需要通过 Click 触发该事件,因此您被迫使用EventArgs对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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