0

我有个问题

我的网格有列

-Name-----------From--------To----------Rate---
Amenity         8/8/2012    8/9/2012    50.00
Amenity         8/6/2012    8/7/2012    50.00
Amenity         8/9/2012    8/10/2012   50.00
Amenity two     7/28/2012   7/31/2012   0.00
Amenity two     8/3/2012    8/4/2012    0.00

我怎样才能在我的前端得到这个结构

-Name-----------From--------To----------Rate---
Amenity         8/8/2012    8/9/2012    50.00
                8/6/2012    8/7/2012    50.00
                8/9/2012    8/10/2012   50.00
Amenity two     7/28/2012   7/31/2012   0.00
                8/3/2012    8/4/2012    0.00

重复Name- 我只想展示一次。我怎样才能做到这一点?

4

2 回答 2

0

假设您使用的是 DataGrid(因为您提到了该RowDataBound事件),那么经典的方法是:

  1. 在页面级别创建一个字符串变量并将其命名为“lastAmenityName”之类的名称。

  2. 将以下代码放入您的 DataRowBound 中(这是来自内存,因此可能不太正确):

..

string amenityName = ((yourobject) e.Row.DataItem).AmenityName;
if(amenityName != lastAmenityName)
     e.Row.Cells[whatevercolumn].Text = amenityName;
else
     e.Row.Cells[whatevercolumn].Text = "";
lastAmenityName = amenityName;
于 2013-02-06T15:28:06.190 回答
0

您可以使用嵌套的 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>
于 2013-02-06T21:14:45.980 回答