10490

Question:
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
I am taking data from one table to GridView
. In GridView i have TemplateField
with Labels which are databound .
You could use <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx" rel="nofollow">RowDataBound
</a> 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 : "";
}
}