在Gridview中显示值列表

时间:2016-03-21 21:10:56

标签: c# asp.net visual-studio gridview repeater

我正在尝试将数据绑定到gridview。在我的列中,我有2列包含值列表,但我的gridview不显示这2列的数据。我做错了什么? 这是我的griview控制器代码“

[DataObjectMethod(DataObjectMethodType.Select, false)]
        public List<ListCourseByProgram> GetCourseByProgram(string programID)
        {
            int intProgramID = Int32.Parse(programID);
            using (HiveMindContext context = new HiveMindContext())
            {

                var results = from data in context.Courses
                              where data.ProgramID == intProgramID
                              select new ListCourseByProgram()
                              {
                                  CourseID = data.CourseID,
                                  ProgramID = intProgramID,
                                  CourseName = data.CourseName,
                                  Active = data.Active,
                                  Credit = data.Credit,
                                  Pre_Requisites = data.Pre_Requisites,
                                  EquivCourseName = from equiName in data.Equivalencies
                                                    where data.CourseID == equiName.CourseID

                                                select new CourseNameEquivalency()
                                                {
                                                    EquivCourseName = equiName.EquivCourseName,

                                                },
                                    EquivCourseID = from equiID in data.Equivalencies
                                                    where data.CourseID == equiID.CourseID

                                                select new CourseIDEquivalency()
                                                {

                                                    EquivCourseID = equiID.EquivCourseID
                                                }

                              };
                return results.ToList();

            }
        }

这是我的Gridview代码:

<asp:GridView ID="Courses_Grid" runat="server" AutoGenerateColumns="False" Width="700px" >

    <Columns>
        <asp:TemplateField Visible="true">
                    <HeaderTemplate>
                       CourseID
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblCourseID" runat="server" Text= '<%#Eval("CourseID") %>'> </asp:Label>

                    </ItemTemplate>
         </asp:TemplateField>


        <asp:BoundField DataField="ProgramID" HeaderText="ProgramID" SortExpression="ProgramID"></asp:BoundField>
        <asp:BoundField DataField="CourseName" HeaderText="CourseName" SortExpression="CourseName"></asp:BoundField>
        <asp:CheckBoxField DataField="Active" HeaderText="Active" SortExpression="Active"></asp:CheckBoxField>
        <asp:BoundField DataField="Credit" HeaderText="Credit" SortExpression="Credit"></asp:BoundField>
        <asp:BoundField DataField="Pre_Requisites" HeaderText="Pre_Requisites" SortExpression="Pre_Requisites"></asp:BoundField>
        <%--<asp:BoundField DataField="EquivCourses" HeaderText="Equivalency" SortExpression="EquivCourses"></asp:BoundField>--%>

         <asp:TemplateField Visible="true">
            <HeaderTemplate>
                EquivalencyCourseName
            </HeaderTemplate>
            <ItemTemplate>              

                <asp:Repeater ID="CourseEquivRepeater" runat="server" DataSource='<%#Eval("EquivCourseNames")%>'>
                    <ItemTemplate>
                        <%# (Container.ItemIndex+1)+"."+ Container.DataItem  %><br />
                    </ItemTemplate>                                                                           
                </asp:Repeater>
            </ItemTemplate>
        </asp:TemplateField>

       <asp:TemplateField Visible="true">
            <HeaderTemplate>
                EquivalencyCourseID
            </HeaderTemplate>
            <ItemTemplate>        
               <asp:Repeater ID="CourseEquivRepeater2" runat="server" DataSource='<%#Eval("EquivCourseIDs")%>'>
                    <ItemTemplate>
                        <%# (Container.ItemIndex+1)+"."+ Container.DataItem  %><br />
                    </ItemTemplate>                                                                           
                </asp:Repeater>
            </ItemTemplate>
        </asp:TemplateField>

    </Columns>

这是我的gidview enter image description here

的图片

2 个答案:

答案 0 :(得分:0)

我认为您需要更正您的查询

EquivCourseName = string.Join("<br />", (from equiName in data.Equivalencies
                  where data.CourseID == equiName.CourseID
                  select equiName.EquivCourseName).ToList()), 
EquivCourseID = string.Join("<br />", (from equiID in data.Equivalencies
                where data.CourseID == equiID.CourseID
                select equiID.EquivCourseID).ToList()), 

答案 1 :(得分:0)

我已经得到了答案。它应该是:

<asp:Repeater ID="CourseEquivRepeater" runat="server" DataSource='<%#Eval("EquivCourseNames")%>'>
       <ItemTemplate>
         <%# (Container.ItemIndex+1)+"."+ Container.DataItem + Eval("EquivCourseName") %><br />
       </ItemTemplate>                                                                           
   </asp:Repeater>

在我的转发器中