1

I have a repeater control nested in a GridView. On GridView Updated I'm trying to DataBind the Repeater. I'm not getting any errors but the databind is not working.

My setup is 2 tables with a many to many relationship. Employee and PrincipleStaff. I am using the navigation property of PrincpleStaffs (hidden join table between Principle Staff and Employee). I am able to update the database through edit but cannot see the updates after update.

Here is my code. GridView Update is working in the database but GridView updated is not filling the repeater control.

aspx:

<asp:GridView ID="AddPrincipleStaff" runat="server" AutoGenerateColumns="False" DataKeyNames="PrincipleStaffID" DataSourceID="PrincipleStaffEmployees" OnRowUpdating="AddPrincipleStaffGridView_RowUpdating">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:TemplateField HeaderText="" SortExpression="PrincipleStaffID">
                <EditItemTemplate>
                    <asp:Label ID="PrincipleStaffIDEditTemplate" runat="server" Text='<%# Eval("PrincipleStaffID") %>' style="display: none;"></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="PrinicpleStaffID" runat="server" Text='<%# Bind("PrincipleStaffID") %>' style="display: none;"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="PrincipleStaffTitle" SortExpression="PrincipleStaffTitle">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("PrincipleStaffTitle") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("PrincipleStaffTitle") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="EmployeeName">
                <EditItemTemplate>
                    <asp:CheckBoxList ID="EmployeesCheckBoxes" runat="server" DataSourceID="EmployeesDataSource" DataTextField="empEmployeeName" DataValueField="empEmployeeID">
                    </asp:CheckBoxList>
                </EditItemTemplate>
                <ItemTemplate>

                    <asp:Repeater runat="server" ID="EmployeeList"></asp:Repeater>

                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ModelFirst;

namespace FactoryWebsite
{
    public partial class AddPrincipalStaffEmployees : System.Web.UI.Page
    {
        FactoryTheaterModelFirstContainer db = new FactoryTheaterModelFirstContainer();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["ProductionID"] != null)
            {
                string stringSession = Session["ProductionID"].ToString();
                int intProductionID = Int32.Parse(stringSession);

                var production = from p in db.Productions
                                 where p.proProductionID == intProductionID
                                 select p.proProductionTitle;

                ProductionTitle.Text = production.FirstOrDefault();
            }

            else
                Response.Redirect("/AddProduction.aspx");
        }

        protected void AddPrincipleStaffGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            foreach (GridViewRow row in AddPrincipleStaff.Rows)
            {
                CheckBoxList cbl = (CheckBoxList)row.FindControl("EmployeesCheckBoxes");
                if (cbl != null)
                {
                    foreach (ListItem item in cbl.Items)
                    {
                        if (item.Selected)
                        {
                            Label PrincipleID = AddPrincipleStaff.Rows[e.RowIndex].FindControl("PrinicpleStaffIDEditTemplate") as Label;

                            int PrincipleStaffID = 0;
                            PrincipleStaffID = Int32.Parse(PrincipleID.Text);
                            var ID = item.Value;
                            int EmployeeID = Int32.Parse(ID);

                            using (var context = new FactoryTheaterModelFirstContainer())
                            {
                                PrincipleStaff principlestaff = context.PrincipleStaffs.Single(s => s.PrincipleStaffID == PrincipleStaffID);
                                Employee employeeid = context.Employees.Single(s => s.empEmployeeID == EmployeeID);
                                principlestaff.Employees.Add(employeeid);
                                context.SaveChanges();
                            }

                        }
                    }
                }

            }
        }

        protected void AddPrincipleStaff_RowUpdated(object sender, GridViewUpdatedEventArgs e)
            {
                foreach (GridViewRow row in AddPrincipleStaff.Rows)
                {
                    Label psid = (Label)FindControl("PrinicpleStaffID");
                    Repeater employees = (Repeater)AddPrincipleStaff.FindControl("EmployeeList") as Repeater;

                    if (psid != null)
                    {

                        int intpsid = 0;
                        intpsid = Int32.Parse(psid.Text);

                        var context = new FactoryTheaterModelFirstContainer();
                        {
                            var query = context.PrincipleStaffs.Where(c => c.PrincipleStaffID == intpsid)
                                           .SelectMany(c => c.Employees)
                                           .Select(a => a.empEmployeeName).ToList();

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

                    }

                }
        }


    }
}
chuckles
  • 11
  • 2
  • Anyone have any suggestions on this? Should I take a different approach here? Thank you for any info anyone can provide. – chuckles Jul 23 '13 at 21:17

1 Answers1

0

I was trying to do the wrong thing here! I needed to have a RowDataBound command. This updates the nested gridview in the parent gridview.

    protected void AddPrincipleStaff_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)// Bind nested grid view with parent grid view
        {

            var psid = DataBinder.Eval(e.Row.DataItem, "PrincipleStaffID");
            int intpsid = 0;
            intpsid = Int32.Parse(psid.ToString());

            using (var context = new FactoryTheaterModelFirstContainer())
            {
                var query = (from c in context.PrincipleStaffs
                             from p in c.Employees
                             where c.PrincipleStaffID == intpsid
                             select new
                             {
                                 Name = p.empEmployeeName
                             }).ToList();

                if (query.Count > 0)
                {
                    GridView childgrd = (GridView)e.Row.FindControl("ListEmployees"); // find nested grid view from parent grid veiw
                    childgrd.DataSource = query;
                    childgrd.DataBind();
                }
            }
        }
chuckles
  • 11
  • 2