Reputation: 1037
I have a class named Student
public class Student
{
public string Name { get; set; }
public List<int> Marks { get; set; }
public Student()
{
}
}
and I need to bind a list of Student to 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();
The gridview shows the name field only. how I can show the list of marks also with name field. (In this all the students have same number of marks, means sometimes 3, or sometimes 5. etc.)
I need show gridview like this
Name Mark1 Mark2 Mark3
Scott 10 20 30
Jon 10 20 30
Upvotes: 0
Views: 5487
Reputation: 1759
Add a custom container as TemplateField
field (if number of marks varies. Example : another GridView
,Repeater
or ListView
) or some Label
or TextBox
as template field(if number of marks is fixed). Override GridView1_RowDataBound and set values for the container DataSource
or the labels to show the marks.
in 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>
in 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();
}
}
Upvotes: 3
Reputation: 2696
Try the following code:
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();
}
}
}
Upvotes: 0
Reputation: 170
Example:
<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>
Code:
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();
}
I do not test it
Upvotes: 1