项模板中的网格视图在主网格视图中绑定一次被覆盖 行数据绑定方法

本文关键字:视图 网格 一次 覆盖 方法 数据绑定 绑定 | 更新日期: 2023-09-27 17:55:44

在一种情况下被击中。 创建了包含多个网格的网格。 代码如下

<asp:GridView CssClass="table-responsive" ID="gridReport" runat="server"
                                AutoGenerateColumns="false"
                                DataKeyNames="sales_id" OnRowDataBound="gridReport_RowDataBound">
                                <Columns>
                                    <asp:BoundField DataField="Partno" HeaderText="PART No.">
                                        <ItemStyle />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="Partname" HeaderText="PART NAME">
                                        <ItemStyle />
                                    </asp:BoundField>
                                    <asp:TemplateField HeaderText="ANNUAL QTY (in Nos.)">
                                        <ItemTemplate>
                                            <asp:GridView ID="Grid_YEAR" runat="server" AutoGenerateColumns="true">
                                            </asp:GridView>
                                        </ItemTemplate>
                                    </asp:TemplateField>

                                    <asp:TemplateField HeaderText="Raw Material">
                                        <ItemTemplate>
                                            <asp:GridView ID="Grid_RM" runat="server" AutoGenerateColumns="true">
                                            </asp:GridView>
                                        </ItemTemplate>
                                    </asp:TemplateField>
 </Columns>
                                </asp:GridView>

并在代码隐藏中

protected void gridReport_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        string id = "";
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                id = gridReport.DataKeys[e.Row.RowIndex].Value.ToString();

                GridView gvGauges = e.Row.FindControl("Grid_YEAR") as GridView;
                DataTable dt_years = getdata(string.Format("SELECT * FROM tbl_sales_parts where sales_id={0} and sale_part_id='{1}'", id, id + "_" + e.Row.RowIndex));
                if (dt_years.Rows.Count > 0)
                {
                    gvGauges.DataSource = dt_years;
                    gvGauges.DataBind();
                }

                GridView Grid_RM = e.Row.FindControl("Grid_YEAR") as GridView;
                DataTable dt_RM = getdata(string.Format("SELECT *  FROM tbl_RM where sale_id={0} and sale_part_id='{1}'", id, id + "_" + e.Row.RowIndex));
                if (dt_RM.Rows.Count > 0)
                {
                    Grid_RM.DataSource = dt_RM;
                    Grid_RM.DataBind();
                }


            }
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "message", "$.prompt('" + ex.Message + "ïd - " + id + "')", true);
        }
    }

但我只得到第二个子网格,即"Grid_RM"正在绑定,它覆盖第一个子网格,即"grid_year"网格列。 由于某些列名需要动态生成,我创建了将 coloumn自动生成设置为 true 的网格。

更新

以前将相同的网格初始化为时间,因此它被覆盖。 在@Tim施梅尔特的帮助下得到了解决方案。

更新的代码如下 -

protected void gridReport_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        string id = "";
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                id = gridReport.DataKeys[e.Row.RowIndex].Value.ToString();

                GridView gvGauges = e.Row.FindControl("Grid_YEAR") as GridView;
                DataTable dt_years = getdata(string.Format("SELECT * FROM tbl_sales_parts where sales_id={0} and sale_part_id='{1}'", id, id + "_" + e.Row.RowIndex));
                if (dt_years.Rows.Count > 0)
                {
                    gvGauges.DataSource = dt_years;
                    gvGauges.DataBind();
                }

                GridView Grid_RM = e.Row.FindControl("Grid_RM") as GridView;
                DataTable dt_RM = getdata(string.Format("SELECT *  FROM tbl_RM where sale_id={0} and sale_part_id='{1}'", id, id + "_" + e.Row.RowIndex));
                if (dt_RM.Rows.Count > 0)
                {
                    Grid_RM.DataSource = dt_RM;
                    Grid_RM.DataBind();
                }


            }
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "message", "$.prompt('" + ex.Message + "ïd - " + id + "')", true);
        }
    }

项模板中的网格视图在主网格视图中绑定一次被覆盖 行数据绑定方法

您正在从同一引用e.Row.FindControl("Grid_YEAR")初始化不同的网格视图(gvGaugesGrid_RM)。

相反,您希望:

GridView gvGauges = e.Row.FindControl("Grid_YEAR") as GridView;
// ...
GridView Grid_RM = e.Row.FindControl("Grid_RM") as GridView;