【问题标题】:how to show a listview inside a gridview control's item template.如何在 gridview 控件的项目模板中显示列表视图。
【发布时间】:2012-04-07 21:13:19
【问题描述】:

如何在 gridview 控件的项目模板中显示列表视图。
gridview 将列出 table_bill 中的所有 bill_id,列表视图将绑定 table_bill_details 中具有特定 item_bill_id 的所有 item_id 和数量。
table_bill 模式

  • bill_id(主键)
  • 账单日期
  • bill_customer_id(此表的外键,源表为table_customer)
  • table_bill_details 架构

  • item_id(主键)
  • 数量
  • item_bill_id(此表的外键,源表为table_bill)

  • 我需要在用户界面中,如下图所示

    【问题讨论】:

    • 为什么要将 ListView 放在 GridView 中?这就像给猪涂口红:/
    • @IrishChieftain :: 我需要它因为 table_bill 和 table_bill_details 之间存在“一对多”的关系。这种技术将有助于根据关系将 table_bill_details 的记录与 table_bill 一起分组。无论如何,我已经实现了我的答案。我会尽快发布答案。
    • 数据库模式与您如何显示数据无关。使用 ListView 模板。

    标签: asp.net listview gridview


    【解决方案1】:

    我终于得到了答案。 只需按以下方式操作...

    在 .aspx 文件中


    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
            <Columns> 
                <asp:TemplateField>
                    <ItemStyle BackColor="#C2D88B" Width="250px" />
                    <ItemTemplate>
                        <div class="id">
                            <asp:Label ID="Label3" runat="server" Text='<%# Eval("bill_id") %>' ></asp:Label>
                        </div>
                        <div class="ex">
                            <p>
                                <asp:ListView ID="ListView1" runat="server">
                                    <ItemTemplate>
                                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("item_id") %>'></asp:Label>
                                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("quantity") %>'></asp:Label>
                                    </ItemTemplate>
                                    <ItemSeparatorTemplate>
                                    <br />                                        
                                    </ItemSeparatorTemplate>
                                </asp:ListView>
                            </p>
                        </div>
                    </ItemTemplate>
                </asp:TemplateField>                 
            </Columns>
        </asp:GridView>
    

    在 aspx.cs 文件中


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataSet ds = new DataSet();
            DataTable bill = new DataTable();
            bill.TableName = "cc";
    
            DataTable details = new DataTable();
            details.TableName = "ii";
    
            //Run necesserry commands to fill cc with values from table_bill & ii with values from table_bill_details
    
            ds.Tables.Add(catogory);
            ds.Tables.Add(item);
            DataRelation rel = new DataRelation("test", ds.Tables["cc"].Columns["bill_id"], ds.Tables["ii"].Columns["bill_id"]);
            ds.Relations.Add(rel);
            this.GridView1.DataSource = ds.Tables["cc"];
            GridView1.DataBind();
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ListView inner = e.Row.FindControl("ListView1") as ListView;
            DataRowView drv = e.Row.DataItem as DataRowView;
            DataRow[] rows = drv.Row.GetChildRows("test");
            ArrayList lst = new ArrayList();
            for (int i = 0; i < rows.Length; i++)
            {
                Item ii = new Item(rows[i][2].ToString(), rows[i][1].ToString(), rows[i][0].ToString());
                lst.Add(ii);
            }
    
            inner.DataSource = lst;
            inner.DataBind();
    
            //drv.Row.
    
        }
    }
    
    class Item
    {
        string quantity;
    
        public string Quantity
        {
            get { return quantity;}
            set { quantity = value; }
        }
        string item_id;
    
        public string Bill_id
        {
            get { return item_id;}
            set { item_id = value; }
        }
        string bill_id;
    
        public string Bill_id
        {
            get { return bill_id;}
            set { bill_id = value; }
        }
    
        public Item(string quantity, string bill_id)
        {
            this.quantity = quantity;
            this.item_id = item_id;
            this.bill_id = bill_id;
        }
    
    }
    

    这就是我想要的。谢谢大家。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      • 2012-10-02
      相关资源
      最近更新 更多