如何使用RowDataBound显示这样的网格数据

时间:2013-02-06 15:16:47

标签: asp.net

我有一个问题

我的网格有列

-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中的重复 - 我只想展示一次。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

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

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

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

  3. ...

    string amenityName = ((yourobject) e.Row.DataItem).AmenityName;
    if(amenityName != lastAmenityName)
         e.Row.Cells[whatevercolumn].Text = amenityName;
    else
         e.Row.Cells[whatevercolumn].Text = "";
    lastAmenityName = amenityName;
    

答案 1 :(得分:0)

您可以使用嵌套的GridView来显示您提到的方式。

enter image description here

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>