合并单元格在具有在asp.net中值相同的GridView [英] merge cells in gridview that has same value in asp.net

查看:129
本文介绍了合并单元格在具有在asp.net中值相同的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的GridView中的数据格式

I have data in following format in gridview

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

不过,我要像如下合并重复值

But i want to merge the repeated value like follows

Region Branch
Gujrat Ahmedabad
       Surat
       Vadodara
Mumbai Dadar
       Andheri
       Borivali

我从一个表取数据,以 GridView控件。在GridView控件我有模板列与标签,这些标签数据绑定。

I am taking data from one table to GridView. In GridView i have TemplateField with Labels which are databound .

推荐答案

您可以使用的 的RowDataBound 设置标签的文本,并检查它是否是一样的previous:

You could use RowDataBound to set the Label's Text and check if it's the same as the previous:

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 : "";
    }
}

这篇关于合并单元格在具有在asp.net中值相同的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆