您可以使用嵌套的 GridView 来显示您提到的方式。
public class User
{
public int UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Date
{
public int DateId { get; set; }
public int UserId { get; set; }
public DateTime FromDate { get; set; }
public DateTime ToDate { get; set; }
public decimal Rate { get; set; }
}
public List<User> Users
{
get
{
return new List<User>
{
new User {UserId = 1, FirstName = "John", LastName = "Doe"},
new User {UserId = 2, FirstName = "Marry", LastName = "Newton"},
new User {UserId = 3, FirstName = "Joe", LastName = "Black"}
};
}
}
public List<Date> Dates
{
get
{
return new List<Date>
{
new Date
{
DateId = 1,
UserId = 1,
FromDate = Convert.ToDateTime("8/8/2012"),
ToDate = Convert.ToDateTime(" 8/9/2012"),
Rate = 50.00M
},
new Date
{
DateId = 2,
UserId = 1,
FromDate = Convert.ToDateTime("8/6/2012"),
ToDate = Convert.ToDateTime("8/7/2012"),
Rate = 50.00M
},
new Date
{
DateId = 3,
UserId = 1,
FromDate = Convert.ToDateTime("8/9/2012"),
ToDate = Convert.ToDateTime("8/10/2012"),
Rate = 50.00M
},
};
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = Users;
GridView1.DataBind();
}
}
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var user = e.Row.DataItem as User;
var gridView2 = ((GridView) e.Row.FindControl("GridView2"));
gridView2.DataSource = Dates.Where(d => d.UserId == user.UserId);
gridView2.DataBind();
}
}
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="False" OnRowDataBound="GridView_RowDataBound">
<Columns>
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:TemplateField>
<ItemTemplate>
<asp:GridView runat="server" ID="GridView2" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="FromDate" HeaderText="From" />
<asp:BoundField DataField="ToDate" HeaderText="To" />
<asp:BoundField DataField="Rate" HeaderText="Rate" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>