Joth
Joth

Reputation: 165

Nested GridViews from nested Lists in asp.net

I have a List of employees where each list item also contains a List of the times (punches) where they accessed different departments.

I'd like to display the data in a GridView (ID: GridView1) that contains BoundField columns for both ID and Name as well as a TemplateField column for a nested GridView (ID: GridView2) which would show the list of Departments, Shifts and Times.

    public class PunchInfo
    {
        public string DepartmentName { get; set; }
        public int Shift { get; set; } 
        public DateTime Time { get; set; }
    }

    public class Employees
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public List<PunchInfo> Punches { get; set; }
    }

I'd like to do it in the .cs file, but I don't know how to go about binding the data to the grid.

            /* EmployeeList is List<Employees> */
            GridView1.DataSource = EmployeeList;  
            GridView gv2 = GridView1.FindControl("GridView2") as GridView;
            /* How to bind the EmployeeList[row_idx].Punches to the nested GridView2 object? */
            GridView1.DataBind();

Thanks

Upvotes: 0

Views: 799

Answers (1)

Li-Jyu Gao
Li-Jyu Gao

Reputation: 940

You don't need to FindControl the GridView2, just add DataSource element in .aspx

My workable example:

.aspx

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
 <Columns>
     <asp:TemplateField HeaderText="Id">
         <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
        </ItemTemplate>
     </asp:TemplateField>
     <asp:TemplateField HeaderText="Name">
        <ItemTemplate>
            <asp:Label ID="Label2" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
        </ItemTemplate>
     </asp:TemplateField>
     <asp:TemplateField HeaderText="Puches">
        <ItemTemplate>
            <asp:GridView ID="DetailGridView" runat="server" AutoGenerateColumns="False" DataSource='<%# Eval("Punches") %>'>
                 <Columns>
                     <asp:TemplateField HeaderText="Department Name">
                         <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%# Eval("DepartmentName") %>'></asp:Label>
                        </ItemTemplate>
                     </asp:TemplateField>
                     <asp:TemplateField HeaderText="Shift">
                        <ItemTemplate>
                            <asp:Label ID="Label4" runat="server" Text='<%# Eval("Shift") %>'></asp:Label>
                        </ItemTemplate>
                     </asp:TemplateField>
                     <asp:TemplateField HeaderText="Time">
                        <ItemTemplate>
                            <asp:Label ID="Label5" runat="server" Text='<%# Eval("Time") %>'></asp:Label>
                        </ItemTemplate>
                     </asp:TemplateField>
                 </Columns>
             </asp:GridView>
        </ItemTemplate>
     </asp:TemplateField>
 </Columns>
</asp:GridView>

.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var employees = new List<Employees>
        {
            new Employees()
            {
                Id = "1",
                Name = "E1",
                Punches = new List<PunchInfo>()
                {
                    new PunchInfo()
                    {
                        DepartmentName = "D1",
                        Shift = 1,
                        Time = DateTime.Now
                    },
                    new PunchInfo()
                    {
                        DepartmentName = "D2",
                        Shift = 2,
                        Time = DateTime.Now
                    },
                }
            },
            new Employees()
            {
                Id = "2",
                Name = "E2",
                Punches = new List<PunchInfo>()
                {
                    new PunchInfo()
                    {
                        DepartmentName = "D3",
                        Shift = 3,
                        Time = DateTime.Now
                    },
                    new PunchInfo()
                    {
                        DepartmentName = "D4",
                        Shift = 4,
                        Time = DateTime.Now
                    },
                }
            }
        };

        GridView2.DataSource = employees;
        GridView2.DataBind();
    }
}

Upvotes: 1

Related Questions