Gridview没有正确绑定:

时间:2014-04-22 09:23:05

标签: c# asp.net .net linq gridview

我有一个LINQ查询,其中我正在使用Group By,我使用以下查询绑定GridView。但是GridView没有正确绑定。在所有行中,它显示如下:

  

System.Linq.Enumerable + WhereSelectEnumerableIterator 2[<>f__AnonymousType652 6 [System.Nullable`1 [System.Int32],System.String,System.String,System.String,System.String,System.Int32],系统.String]

我的aspx页面是

<Columns>
    <asp:TemplateField HeaderText="Sl no." ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
      <div style="margin: 0px auto; width: auto;">
      <asp:Label ID="lblslno" runat="server" Text='<%#Container.DisplayIndex+1 %>'>'></asp:Label>
      <asp:HiddenField ID="hf" runat="server" Value='<%#Eval("EmpId") %>' />
      </div>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField HeaderText="Employee Code." DataField="EmpCode" ItemStyle-HorizontalAlign="Center" />
    <asp:BoundField HeaderText="Employee Name" DataField="Name" />
    <asp:BoundField HeaderText="Department" DataField="DeptName" />
    <asp:BoundField HeaderText="Designation" DataField="DesigName" />
    <asp:TemplateField HeaderText="Total Marks" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <asp:Label ID="lblTotalMarks" runat="server" CssClass="Label" Text='<%#Eval("Total_Mark") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Marks Secured" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
      <asp:Label ID="lblMarksSecured" runat="server" CssClass="Label" Text='<%#Eval("Total_Mark") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>

我背后的代码是这样的

 var query = (from a in TraningScheduleDetails
                     from b in TraningAnshwerSheet
                     from c in TraningAnshwerSheetDetails
                     from d in EmpProfessionalDetails
                     from e in EmpProfessionalJobDetails
                     from f in Department
                     from g in Designation
                     where a.TrainingScheduleDetailId == int.Parse(ddlExamDate.SelectedValue)
                     && a.TrainingScheduleDetailId == b.TrainingScheduleDetailId
                     && b.TrainingAnswerSheetId == c.TrainingAnswerSheetId
                     && b.EmployeeProfessionalDetailId == d.EmployeeProfessionalDetailId
                     && d.EmployeeProfessionalDetailId == e.EmployeeProfessionalDetailId
                     && e.DepartmentId == f.DepartmentId
                     && e.DesignationId == g.DesignationId
                     select new
                     {
                         b.EmployeeProfessionalDetailId,
                         Name = d.FirstName + " " + d.MiddleName + " " + d.LastName,
                         d.EmployeeCode,
                         f.DepartmentName,
                         g.DesignationName,
                         Total_Mark = total_Mark,
                     }).ToList();
        var query2=(from a in query group a by a.EmployeeProfessionalDetailId into b
                    select new
                    {
                        EmpId=b.Key,                            
                        Name=b.Select(c =>c.Name),
                        EmpCode=b.Select(c=>c.EmployeeCode),
                        DeptName=b.Select(c=>c.DepartmentName),
                        DesigName=b.Select(c=>c.DesignationName),
                        Total_Mark=b.Select(c=>c.Total_Mark),                            
                    }).ToList();           
        grdEmployeeDetails.DataSource = query2;
        grdEmployeeDetails.DataBind();

1 个答案:

答案 0 :(得分:0)

为Grid绑定定义一个新类,例如'EmployeeResults'具有必要的属性。 并创建一个绑定到网格的对象列表。

List<EmployeeResults> employeeResults = Linq query. //Cast linq results to EmployeeResults
grdEmployeeDetails.DataSource = employeeResults;
grdEmployeeDetails.DataBind();