Hi,
Here I am merging the Column of same value by comparing the next row. Merging is done by Rowspan Property and Executed in OnRowDataBound Event. To execute this the First column values should be repeated more than once to find the result. Find the code below.
Output preview :
Database structure :
your data Should Contain multiple values of the same value to be Executed and should use order by to the row you are going to merge. here am merging the First colum values.
Here I am merging the Column of same value by comparing the next row. Merging is done by Rowspan Property and Executed in OnRowDataBound Event. To execute this the First column values should be repeated more than once to find the result. Find the code below.
Output preview :
Merging same value Cells in Gridview |
your data Should Contain multiple values of the same value to be Executed and should use order by to the row you are going to merge. here am merging the First colum values.
ASP.Net CODE:
<html>
<head id="Head1" runat="server">
<title>Copyright
@ Fourthbottle</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%">
<tr>
<td align="center">
<asp:GridView ID="grd_popup_details" runat="server"
HeaderStyle-BackColor="LightBlue" AutoGenerateColumns="false" OnRowDataBound="grd_popup_details_RowDataBound">
<Columns>
<asp:BoundField HeaderText="Mobile
Brand" ItemStyle-Width="90px" DataField="Brand" />
<asp:BoundField HeaderText="Model"
ItemStyle-Width="90px"
DataField="Model"
/>
<asp:BoundField HeaderText="Retail
Price" ItemStyle-Width="90px" DataField="Price" />
<asp:BoundField HeaderText="Color"
ItemStyle-Width="90px"
DataField="Color"
/>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
C# CODE:
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
using System.Data;
using
System.Data.SqlClient;
namespace GRD_Merge_Paging
{
public partial class MergeGrid : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
InitialBindingGrid();
}
}
protected
void InitialBindingGrid()
{
DataTable
dt = new DataTable();
SqlConnection
con = new SqlConnection("your Connection String");
string
Query1 = "select Brand,Model,Price,Color FROM Table1 order by Brand
";
con.Open();
SqlDataAdapter
da1 = new SqlDataAdapter(Query1,
con);
da1.Fill(dt);
con.Close();
grd_popup_details.DataSource = dt;
grd_popup_details.DataBind();
}
protected
void grd_popup_details_RowDataBound(object sender, GridViewRowEventArgs
e)
{
int
RowSpan = 2;
for
(int i = grd_popup_details.Rows.Count - 2; i
>= 0; i--)
{
GridViewRow
currRow = grd_popup_details.Rows[i];
GridViewRow
prevRow = grd_popup_details.Rows[i + 1];
if
(currRow.Cells[0].Text == prevRow.Cells[0].Text)
{
currRow.Cells[0].RowSpan =
RowSpan;
prevRow.Cells[0].Visible = false;
RowSpan += 1;
}
else
{
RowSpan = 2;
}
}
}
}
}