In this example i am going to describe how to Merge GridView Cells Or Columns In Gridview Rows Using C#in ASP.NET Containing Same Data or content. For this i m using DataBound Event of gridview, counting total rows and then checking each cells value against value of same cell in previous row and then setting the RowSpan of cells.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderStyle="None" BorderWidth="1px" CellPadding="4" GridLines="Horizontal" ForeColor="Black" Height="119px" DataSourceID="SqlDataSource1" OnDataBound="GridView1_DataBound1"> <Columns> <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" /> <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" /> <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [Country], [State], [City] FROM [Details] ORDER BY [State]"> </asp:SqlDataSource>
protected
void
GridView1_DataBound1(
object
sender, EventArgs e)
{
for
(
int
rowIndex = GridView1.Rows.Count - 2;
rowIndex >= 0; rowIndex--)
{
GridViewRow gvRow = GridView1.Rows[rowIndex];
GridViewRow gvPreviousRow = GridView1.Rows[rowIndex + 1];
for
(
int
cellCount = 0; cellCount < gvRow.Cells.Count;
cellCount++)
{
if
(gvRow.Cells[cellCount].Text ==
gvPreviousRow.Cells[cellCount].Text)
{
if
(gvPreviousRow.Cells[cellCount].RowSpan < 2)
{
gvRow.Cells[cellCount].RowSpan = 2;
}
else
{
gvRow.Cells[cellCount].RowSpan =
gvPreviousRow.Cells[cellCount].RowSpan + 1;
}
gvPreviousRow.Cells[cellCount].Visible =
false
;
}
}
}
}
No comments:
Post a Comment