如何将列表中的列表绑定到ASP.NET中的GridView

时间:2012-04-10 04:40:54

标签: c# asp.net

我有一个名为Student

的班级
public class Student
{
    public string Name { get; set; }
    public List<int> Marks { get; set; }
    public Student()
    {
    }
}

我需要将Student列表绑定到GridView

        List<Student> StudentList = new List<Student>();

        Student stud = new Student();
        stud.Name = "Scott";
        List<int> marks = new List<int>();
        marks.Add(10); 
        marks.Add(20); 
        marks.Add(30);
        stud.Marks = marks;

        StudentList.Add(stud);

        Student stud1 = new Student();
        stud1.Name = "Jon";
        List<int> marks1 = new List<int>();
        marks1.Add(10);
        marks1.Add(20);
        marks1.Add(30);
        stud1.Marks = marks1;

        StudentList.Add(stud1);

        GridView1.DataSource = StudentList;
        GridView1.DataBind();

gridview仅显示名称字段。如何使用名称字段显示标记列表。 (在这个所有的学生都有相同数量的分数,有时意味着3,有时甚至是5.等等)

我需要像这样显示gridview

Name    Mark1  Mark2 Mark3 
Scott   10     20    30
Jon     10     20    30

3 个答案:

答案 0 :(得分:1)

将自定义容器添加为TemplateField字段(如果标记数量不同。例如:另一个GridViewRepeaterListView)或某些LabelTextBox作为模板字段(如果标记的数量是固定的)。覆盖GridView1_RowDataBound并设置容器DataSource的值或标签以显示标记。

在aspx中: -

<asp:GridView ID="GridView1" runat="server" 
        onselectedindexchanged="GridView1_SelectedIndexChanged" 
        onrowdatabound="GridView1_RowDataBound">
        <Columns>
         <asp:BoundField DataField="Name" HeaderText="Name" />
          <asp:TemplateField HeaderText="Marks">
                   <ItemTemplate>
                       <asp:DataList runat="server" ID="DataList1"    RepeatDirection="Horizontal" >
                       <ItemTemplate>
                       <%# Container.DataItem.ToString() %>

                       </ItemTemplate>
                       </asp:DataList>
                   </ItemTemplate>
               </asp:TemplateField>
        </Columns>
    </asp:GridView>
在cs中

: -

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            var student = e.Row.DataItem as Student;
            var dataList = e.Row.FindControl("DataList1") as DataList;
            dataList.DataSource = student.Marks;

            dataList.DataBind();
        }
    }

答案 1 :(得分:1)

示例:

<asp:GridView ID="grid1" runat="server" AutoGenerateColumns="false" Width="100%">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:GridView ID="grid2" runat="server" AutoGenerateColumns="False" Width="100%">
                <Columns>
                    <asp:BoundField DataField="Mark" HeaderText="Mark" />
                </Columns>
            </asp:GridView>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

代码:

protected override void OnInit(EventArgs e)
{
  grid1.RowDataBound += grid1_RowDataBound;
}

void grid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  var grid2 = (GridView)e.Item.FindControl("grid2");
  grid2.DataSource = StudentList.Where(w => w.Name = (e.Item.DataItem as Student).Name);
  grid2.Bind();
}

我不测试它

答案 2 :(得分:0)

请尝试以下代码:

protected void gridStudentDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
 Student studentObj = (Student)StudentList[e.Row.RowIndex];
 if (studentObj != null)
  {
   DropDownList ddlMarks = (DropDownList)e.Row.FindControl("ddlMarks");
   if(ddlMarks != null)
   {
    ddlMarks.DataSource = studentObj.Marks;
    ddlMarks.DataBind();
   }
  }
 }