2012-07-24 64 views
1

我有以下格式數據的GridView合併單元格中有在asp.net相同的值

Region Branch 
Gujrat Ahmedabad 
Gujrat Surat 
Gujrat Vadodara 
Mumbai Dadar 
Mumbai Andheri 
Mumbai Borivali 

gridview的,但我想合併重複的值如下一樣

Region Branch 
Gujrat Ahmedabad 
     Surat 
     Vadodara 
Mumbai Dadar 
     Andheri 
     Borivali 

我服用數據從一張表格輸入GridView。在GridView我有TemplateField與數據綁定的標籤。

+0

您正在使用C#或VB.NET? – 2012-07-24 15:56:55

+0

使用你的網格視圖的行數據綁定事件或在將其分配給gridview之前首先從數據源(數據表)中挑選重複的值 – 2012-07-24 15:58:31

+0

我認爲你看起來像這樣:** http://www.codeproject.com /條/ 249155 /行 - 和 - 列 - 合併 - 在-ASP-NET-GridView控件,對照** – 2012-07-24 16:47:00

回答

1

你可以使用RowDataBound設置標籤的文本,並檢查它是否是一樣的前面:

protected void Grid_RowDataBound(Object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex > 0) 
    { 
     GridView grid = (GridView)sender; 
     var rowView = (DataRowView)e.Row.DataItem; 
     int lastRowIndex = e.Row.RowIndex - 1; 
     var lastRowView = (DataRowView)grid.Rows[lastRowIndex].DataItem; 
     // replace Region with the correct column name 
     String region = rowView.Row.Field<String>("Region"); 
     String lastRegion = lastRowView.Row.Field<String>("Region"); 
     // replace LblRegion with the correct ID of your Label 
     Label label = (Label)e.Row.FindControl("LblRegion"); 
     label.Text = region != lastRegion ? region : ""; 
    } 
} 
相關問題