Hi,
Here I have coded for merging the same Column values of Gridview While comparing with the Next Column. Everything is fine, when there is lot of records Paging should be enabled for the Grid. For the merged Gridview Same First column data cannot be divided across the pages so we can’t enable default paging of setting some default value for paging, have written some manual paging to come across the all the problems. Find the code below. Merging the Column cells of same value in Gridview
Output Preview :
Database Structure :
ASPX CODE:
Here I have coded for merging the same Column values of Gridview While comparing with the Next Column. Everything is fine, when there is lot of records Paging should be enabled for the Grid. For the merged Gridview Same First column data cannot be divided across the pages so we can’t enable default paging of setting some default value for paging, have written some manual paging to come across the all the problems. Find the code below. Merging the Column cells of same value in Gridview
Output Preview :
Merged Gridview paging |
ASPX CODE:
<html>
<head 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>
<br />
<asp:PlaceHolder ID="phlinkbtns" runat="server"></asp:PlaceHolder>
</td>
</tr>
</table>
<asp:HiddenField ID="hdnpagecount" runat="server" />
<asp:Label ID="lblstatus" ClientIDMode="Static" runat="server"></asp:Label>
</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 MobileDeatail : System.Web.UI.Page
{
DataTable
dt = new DataTable();
SqlConnection
con = new SqlConnection("Connection String");
/*Adding
Rownumber column for the selected rows */
string
Query1 = "select row_number() over(order by Brand) as
Rownumber, Brand ,Model,Price, Color FROM Table1 order by Brand";
int
PageNum = 1;
int
PagSize = 7;/* to change minimum page Size change
here */
int
diff = 0;
int i =
0;
string
Curr = "";
string
Nxt = "";
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
/*
calls for the First time When page loaded */
InitialBindingGrid();
}
else
{
/*
Creating the link button control and its click events for paging*/
CreateControl();
}
}
/* First time
when page loads*/
protected
void InitialBindingGrid()
{
con.Open();
Dictionary<int, int>
DctPaging = new Dictionary<int, int>();
SqlDataAdapter
da1 = new SqlDataAdapter(Query1,
con);
try
{
da1.Fill(dt);
con.Close();
if
(dt.Rows.Count > 0)
{
if
(dt.Rows.Count > PagSize)
{
for (i = PagSize; i <= dt.Rows.Count; i++)
{
Curr = dt.Rows[i -
1]["Brand"].ToString();
if (i < dt.Rows.Count)
{
Nxt =
dt.Rows[i]["Brand"].ToString();
diff =
dt.Rows.Count - i;
if
(Curr != Nxt)
{
DctPaging.Add(PageNum, i);
PageNum =
PageNum + 1;
i = i +
PagSize;
if (i >= dt.Rows.Count)
{
DctPaging.Add(PageNum, i);
break;
}
}
}
else
{
DctPaging.Add(PageNum, i);
break;
}
}
hdnpagecount.Value =
DctPaging.Count.ToString();
CreateControl();
DataTable dtw = new DataTable();
dtw.Columns.Add("Rownumber");
dtw.Columns.Add("Brand");
dtw.Columns.Add("Model");
dtw.Columns.Add("Price");
dtw.Columns.Add("Color");
int Startval = 0;
int
Lastval = DctPaging[1];
DataRow[] Pagerows;
Pagerows=dt.Select("Rownumber >"+Startval+" and Rownumber <="+Lastval);
foreach (DataRow row in Pagerows)
{
dtw.ImportRow(row);
}
grd_popup_details.DataSource = dtw;
grd_popup_details.DataBind();
}
else
{
grd_popup_details.DataSource = dt;
grd_popup_details.DataBind();
}
}
else
{
lblstatus.Text = "No records found for Department Sync";
}
}
catch
(Exception es)
{
lblstatus.Text = es.Message;
}
}
/* When
paging is performed*/
protected
void BindGridview(int
pagenumb)
{
SqlDataAdapter
da1 = new SqlDataAdapter(Query1,
con);
Dictionary<int, int>
DctPaging = new Dictionary<int, int>();
try
{
da1.Fill(dt);
for
(i = PagSize; i <= dt.Rows.Count; i++)
{
Curr = dt.Rows[i - 1]["Brand"].ToString();
if
(i < dt.Rows.Count)
{
Nxt = dt.Rows[i]["Brand"].ToString();
diff = dt.Rows.Count - i;
if (Curr != Nxt)
{
DctPaging.Add(PageNum, i);
PageNum = PageNum +
1;
i = i + PagSize;
if (i >= dt.Rows.Count)
{
DctPaging.Add(PageNum, dt.Rows.Count);
break;
}
}
}
else
{
DctPaging.Add(PageNum,
dt.Rows.Count);
break;
}
}
hdnpagecount.Value =
DctPaging.Count.ToString();
DataTable
dtw = new DataTable();
dtw.Columns.Add("Rownumber");
dtw.Columns.Add("Brand");
dtw.Columns.Add("Model");
dtw.Columns.Add("Price");
dtw.Columns.Add("Color");
if
(pagenumb != 1)
{
int
Startval = DctPaging[pagenumb - 1];
int
Lastval = DctPaging[pagenumb];
DataRow[]
Pagerows;
Pagerows=dt.Select("Rownumber >"+Startval+" and Rownumber <="+Lastval);
foreach
(DataRow row in
Pagerows)
{
dtw.ImportRow(row);
}
grd_popup_details.DataSource
= dtw;
grd_popup_details.DataBind();
}
else
{
int
Startval = 0;
int
Lastval = DctPaging[pagenumb];
DataRow[]
Pagerows;
Pagerows=dt.Select("Rownumber >"+Startval+" and Rownumber <="+Lastval);
foreach
(DataRow row in
Pagerows)
{
dtw.ImportRow(row);
}
grd_popup_details.DataSource
= dtw;
grd_popup_details.DataBind();
}
con.Close();
}
catch
(Exception es)
{
lblstatus.Text =
es.Message.ToString();
}
}
/* creating
pagenumbers for paging*/
private
void CreateControl()
{
if
(hdnpagecount.Value != "")
{
for
(int i = 1; i <= Convert.ToInt32(hdnpagecount.Value);
i++)
{
LinkButton
lb = new LinkButton();
lb.Style.Add("text-decoration", "none");
lb.Text = Convert.ToString(i) + "
";
lb.ID = Convert.ToString(i);
lb.CommandArgument = Convert.ToString(i);
lb.CommandName = Convert.ToString(i);
lb.Command += new CommandEventHandler(lb_Click);
phlinkbtns.Controls.Add(lb);
lb.ForeColor = System.Drawing.Color.Teal;
}
}
}
/* clickevent
for the paging */
protected
void lb_Click(object
sender, CommandEventArgs e)
{
LinkButton
lnk = sender as LinkButton;
lnk.Style.Add("text-decoration", "none");
lnk.ForeColor = System.Drawing.Color.Chocolate;
BindGridview(Convert.ToInt32(e.CommandArgument));
}
/* Merging
the Same value Records*/
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;
}
}
}
}
}