2013-02-06 42 views
0

顯示這樣的柵格數據我有一個問題如何使用的RowDataBound

我的網格有列

-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 - 我只想展示一次。我怎樣才能做到這一點?

+1

你在使用網格控件? GridView?一個DataGrid?第三方網格? –

回答

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; 
+0

string amenityName =((yourobject)e.Row.DataItem).AmenityName;在這一行什麼是由我的對象...保護無效grdHotelAmenity_RowDataBound(對象發件人,GridViewRowEventArgs e) { 字符串amenityName =((grdHotelAmenity)e .Row.DataItem).AmenityName; (amenityName!= lastAmenityName) e.Row.Cells [1] .Text = amenityName;其他 e.Row.Cells [1] .Text =「 lastAmenityName = amenityName;這個是我的代碼 –

+0

「yourobject」我的意思是'e.Row.DataItem'的任何類型的項目:DataRow',DataRowView'或EntityFramework對象都有自己的名字。對困惑感到抱歉。 –

+0

非常感謝Ann L. –

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> 
相關問題