合并gridview中在asp.net中具有相同值的单元格
我在gridview中有以下格式的数据合并gridview中在asp.net中具有相同值的单元格,asp.net,gridview,Asp.net,Gridview,我在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 我正在从
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
。您可以使用设置标签的文本并检查它是否与前面的相同:
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 : "";
}
}
protectedvoid Grid\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow&&e.Row.RowIndex>0)
{
GridView网格=(GridView)发送方;
var rowView=(DataRowView)e.Row.DataItem;
int lastRowIndex=e.Row.RowIndex-1;
var lastRowView=(DataRowView)grid.Rows[lastRowIndex].DataItem;
//用正确的列名替换区域
字符串region=rowView.Row.Field(“region”);
字符串lastRegion=lastRowView.Row.Field(“区域”);
//用标签的正确ID替换LblRegion
Label Label=(Label)e.Row.FindControl(“LblRegion”);
label.Text=region!=lastRegion?区域:“”;
}
}
您可以使用设置标签的文本并检查其是否与前面的相同:
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 : "";
}
}
protectedvoid Grid\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow&&e.Row.RowIndex>0)
{
GridView网格=(GridView)发送方;
var rowView=(DataRowView)e.Row.DataItem;
int lastRowIndex=e.Row.RowIndex-1;
var lastRowView=(DataRowView)grid.Rows[lastRowIndex].DataItem;
//用正确的列名替换区域
字符串region=rowView.Row.Field(“region”);
字符串lastRegion=lastRowView.Row.Field(“区域”);
//用标签的正确ID替换LblRegion
Label Label=(Label)e.Row.FindControl(“LblRegion”);
label.Text=region!=lastRegion?区域:“”;
}
}
使用网格视图的行数据绑定事件,或在将其分配给gridview之前,首先从数据源(数据表)中排序重复的值。我认为您看起来是这样的:使用网格视图的行数据绑定事件,或在将其分配给gridview之前,首先从数据源(数据表)中排序重复的值我觉得你看起来像这样: