如何将数据绑定到嵌套的datagrid中?和使父级可折叠的提示



我有一个带有以下列的工作数据,我已经删除了诸如"清洁式"

之类的东西
<asp:DataGrid id="dgLotDetails" runat="server">
    <Columns>
        <asp:BoundColumn DataField="ItemNumber" SortExpression="ItemNumber" ReadOnly="True" HeaderText="ItemNumber"></asp:BoundColumn>
        <asp:BoundColumn DataField="ItemDescription" SortExpression="ItemDescription" ReadOnly="True" HeaderText="ItemDesc" Visible="False"></asp:BoundColumn>
        <asp:BoundColumn DataField="whse" SortExpression="whse" ReadOnly="True" HeaderText="Warehouse"></asp:BoundColumn>
        <asp:BoundColumn DataField="Bundle" SortExpression="Bundle" ReadOnly="True" HeaderText="Bundle"></asp:BoundColumn>
        <asp:BoundColumn DataField="Size" SortExpression="Size" ReadOnly="True" HeaderText="Size"></asp:BoundColumn>
        <asp:BoundColumn DataField="Weight" SortExpression="Weight" ReadOnly="True" HeaderText="Weight"></asp:BoundColumn>
    </Columns>
</asp:DataGrid>

对于数据绑定,我正在使用以下查询并将其绑定到dglotdetails,该表包含所需的所有信息。数据座是由按钮单击事件触发的。

SELECT * FROM lot-details WHERE lot='" + this.txtLot.Text + "' and Cont='" + this.txtCont.Text + "'

由于有时数据杂志太混乱了,无法在一个单个网格中显示,所以我试图将其整合到以下嵌套的datagrid中。

<asp:DataGrid id="dgLotDetails" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundColumn DataField="ItemNumber" SortExpression="ItemNumber" ReadOnly="True" HeaderText="ItemNumber"></asp:BoundColumn>
        <asp:BoundColumn DataField="ItemDescription" SortExpression="ItemDescription" ReadOnly="True" HeaderText="ItemDesc" Visible="False"></asp:BoundColumn>
        <asp:BoundColumn DataField="whse" SortExpression="whse" ReadOnly="True" HeaderText="Warehouse"></asp:BoundColumn>
        <asp:BoundColumn DataField="Bundle" SortExpression="Bundle" ReadOnly="True" HeaderText="Bundle"></asp:BoundColumn>
        <asp:TemplateColumn>
            <ItemTemplate>
                <asp:DataGrid ID="dgLotDetailsExpand" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundColumn DataField="Size" SortExpression="Size" ReadOnly="True" HeaderText="Size"></asp:BoundColumn>
                        <asp:BoundColumn DataField="Weight" SortExpression="Weight" ReadOnly="True" HeaderText="Weight"></asp:BoundColumn>
                    </Columns>
                </asp:DataGrid>
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
</asp:DataGrid>

我似乎无法弄清楚,我应该如何编写查询(或者我需要为此创建一个存储?

另外,在ParentGrid可扩展/可折叠中制作行的最佳方法是什么?

您确定需要一个嵌套网格吗?这呢:

<script src="Scripts/jquery-3.3.1.js"></script>
<script>
    $(document).ready(function () {
        $(".control").click(function () {
            $(this).next(".detail").toggle();
        });
    });
</script>
<style>
    .control {
        cursor: pointer;
    }
    .detail {
        position: fixed;
        margin: 1ex;
        padding: 1ex;
        border: 1px solid #eee;
        background-color: #FFF;
    }
</style>
<asp:DataGrid id="dgLotDetails" runat="server" AutoGenerateColumns="false" DataKeyNames="ItemNumber" OnRowDataBound="GridViewMain_RowDataBound">
<Columns>
    <asp:BoundColumn DataField="ItemNumber" SortExpression="ItemNumber" ReadOnly="True" HeaderText="ItemNumber"></asp:BoundColumn>
    <asp:BoundColumn DataField="ItemDescription" SortExpression="ItemDescription" ReadOnly="True" HeaderText="ItemDesc" Visible="False"></asp:BoundColumn>
    <asp:BoundColumn DataField="whse" SortExpression="whse" ReadOnly="True" HeaderText="Warehouse"></asp:BoundColumn>
    <asp:BoundColumn DataField="Bundle" SortExpression="Bundle" ReadOnly="True" HeaderText="Bundle"></asp:BoundColumn>
    <asp:TemplateColumn>
        <ItemTemplate>
            <div class="control">Show</div>
            <div class="detail" style="display:none;">
                <asp:DataGrid ID="dgLotDetailsExpand" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundColumn DataField="Size" SortExpression="Size" ReadOnly="True" HeaderText="Size"></asp:BoundColumn>
                        <asp:BoundColumn DataField="Weight" SortExpression="Weight" ReadOnly="True" HeaderText="Weight"></asp:BoundColumn>
                    </Columns>
                </asp:DataGrid>
            </div>
        </ItemTemplate>
    </asp:TemplateColumn>
</Columns>

好...和Templete列中的嵌套网格:

<asp:DataGrid id="dgLotDetails" runat="server" AutoGenerateColumns="false" DataKeyNames="ItemNumber" OnRowDataBound="GridViewMain_RowDataBound">
    <Columns>
        <asp:BoundColumn DataField="ItemNumber" SortExpression="ItemNumber" ReadOnly="True" HeaderText="ItemNumber"></asp:BoundColumn>
        <asp:BoundColumn DataField="ItemDescription" SortExpression="ItemDescription" ReadOnly="True" HeaderText="ItemDesc" Visible="False"></asp:BoundColumn>
        <asp:BoundColumn DataField="whse" SortExpression="whse" ReadOnly="True" HeaderText="Warehouse"></asp:BoundColumn>
        <asp:BoundColumn DataField="Bundle" SortExpression="Bundle" ReadOnly="True" HeaderText="Bundle"></asp:BoundColumn>
        <asp:TemplateColumn>
            <ItemTemplate>
                <asp:DataGrid ID="dgLotDetailsExpand" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundColumn DataField="Size" SortExpression="Size" ReadOnly="True" HeaderText="Size"></asp:BoundColumn>
                        <asp:BoundColumn DataField="Weight" SortExpression="Weight" ReadOnly="True" HeaderText="Weight"></asp:BoundColumn>
                    </Columns>
                </asp:DataGrid>
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
</asp:DataGrid>

代码背后:

protected void GridViewMain_RowDataBound (object Sender, GridViewRowEventArgs Ea) 
{
    if (Ea.Row.RowType == DataControlRowType.DataRow) 
    {
        string _key = GridView1.DataKeys[Ea.Row.RowIndex].Value.ToString();
        var _nestedGrid = Ea.Row.FindControl ("dgLotDetailsExpand") as GridView;
        if (_nestedGrid == null)
            throw new Exception("NestedGrid's missing...");
        _nestedGrid.DataSource = GetNestedData(_key);
        _nestedGrid.DataBind();
    }
}
private IEnumerable<Detail> GetNestedData (string Lot, string Cont) 
{
    var _result = new List<Detail> ();
    using (SqlConnection _conn = new SqlConnection (ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString)) 
    {
        _conn.Open ();
        using (var _command = new SqlCommand ("SELECT * FROM lot-details WHERE lot=@lot and Cont=@Cont", _conn)) 
        {
            _command.Parameters.Add (new SqlParameter ("lot", Lot));
            _command.Parameters.Add (new SqlParameter ("Cont", Cont));
            using (var _da = new SqlDataAdapter (_command)) 
            {
                var _dataTable = new DataTable ();
                _da.Fill (_dataTable);
                foreach (DataRow _aktRow in _dataTable.Rows) 
                {
                    _result.Add (new Detail () 
                    {
                        Size = _aktRow["Size"].ToString (),
                        Weight = _aktRow["Weight"].ToString (),
                    });
                }
            }
        }
    }
    return _result;
}
class Detail {
    public string Size { get; set; }
    public string Weight { get; set; }
}

但是,当您想做更大的事情时,请查看:Telerik Grid -WebForms。它可以节省您很多时间...

最新更新