0

我有一个名为 Student 的班级

public class Student
{
    public string Name { get; set; }
    public List<int> Marks { get; set; }
    public 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();

网格视图仅显示名称字段。我如何也可以使用名称字段显示标记列表。(在此所有学生的分数相同,有时是 3,有时是 5。等等。)

我需要像这样显示gridview

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

3 回答 3

3

添加一个自定义容器作为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();
        }
    }
于 2012-04-10T04:48:39.827 回答
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();
}

我不测试它

于 2012-04-10T05:04:37.583 回答
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();
   }
  }
 }
于 2012-04-10T05:19:30.033 回答