Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show a listview inside a gridview control's item template.

how to show a listview inside a gridview control's item template.
the gridview will list all bill_id from table_bill and list view will bind all the item_id and quantity having a specific item_bill_id from table_bill_details.
table_bill schema

  • bill_id (Primary Key)
  • bill_date
  • bill_customer_id (Foreign Key of this table, Origin table is table_customer)
  • table_bill_details schema

  • item_id (Primary Key)
  • quantity
  • item_bill_id (Foreign Key of this table, Origin table is table_bill)

  • I needed in user interface as shown in the following image

    This is what i needed in user interface

    like image 662
    Krishanu Dey Avatar asked Apr 07 '12 21:04

    Krishanu Dey


    People also ask

    What is the difference between ListView and GridView in ASP NET?

    Programmatic access to the ListView object model to dynamically set properties, handle events, and so on. Multiple key fields. GridView Displays the values of a data source in a table where each column represents a field and each row represents a record.

    How do I change from list view to GridView?

    You can switch between the classic List View and Grid View by tapping the grid/list icon by tapping on the top left corner. Step 1: To change to Grid View, click the Grid View icon on the top left corner of the ribbon in the home screen.

    What is difference between ListView and GridView?

    The main difference between ListView and GridView is how it lays out its child. With ListView you are laying your children one by one either vertically or horizontally only. With GridView, its a combination of both. It lays its children horizontally first.

    What is the difference between GridView and ListView in Windows app development?

    Answers. They both do essentially the same thing (most of the actual code is in the shared ListViewBase class), except that the GridView lays out a horizontal grid of items and the ListView lays out a vertical list of items.


    1 Answers

    finally I have got my answer. Just do it as follows...

    In .aspx file


    <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>
    

    On aspx.cs file


    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;
        }
    
    }
    

    That's all I wanted. Thanks everyone.

    like image 126
    Krishanu Dey Avatar answered Sep 22 '22 02:09

    Krishanu Dey