1

我在gridview中有以下格式的数据

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

但我想合并重复的值,如下所示

Region Branch
Gujrat Ahmedabad
       Surat
       Vadodara
Mumbai Dadar
       Andheri
       Borivali

我正在从一张表中获取数据GridView。在 GridView 我有TemplateField数据绑定的标签。

4

1 回答 1

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 : "";
    }
}
于 2012-07-24T16:09:32.283 回答