Hi Hazel,
Instead of calling MergeGridviewRows(gvSiriAudit) on gvAuditPlanSearch_PreRender call MergeGridviewRows after BindGridSiriAudit().
Refer below sample.
HTML
<asp:GridView ID="gvSiriAudit" runat="server" OnRowDataBound="gvSiriAudit_RowDataBound" CssClass="mGridA" OnPreRender="gvAuditPlanSearch_PreRender"
AutoGenerateColumns="False" ShowHeaderWhenEmpty="true" DataKeyNames="GROUP">
<%--OnDataBound="gvSiriAudit_DataBound"--%>
<EmptyDataRowStyle HorizontalAlign="Center" ForeColor="Red" />
<EmptyDataTemplate>
<strong><span>NO DATA</span></strong>
</EmptyDataTemplate>
<%--<AlternatingRowStyle BackColor="#dedede"></AlternatingRowStyle>--%>
<Columns>
<%--<asp:BoundField HeaderText="Id" DataField="Id" ItemStyle-HorizontalAlign="Center"></asp:BoundField>--%>
<asp:BoundField HeaderText="GROUP" DataField="Group" ItemStyle-HorizontalAlign="Center"></asp:BoundField>
<asp:TemplateField HeaderText="NAME" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="350px">
<ItemTemplate>
<asp:GridView ID="gvStaffName" runat="server" ShowHeader="false" Width="100%">
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="CLAUSE" DataField="Clause"></asp:BoundField>
<asp:BoundField HeaderText="PROCESS" DataField="Process" ItemStyle-HorizontalAlign="Center"></asp:BoundField>
<asp:BoundField HeaderText="SUBPROCESS" DataField="SubProcess" ItemStyle-HorizontalAlign="Center"></asp:BoundField>
<asp:BoundField HeaderText="DEPARTMENT" DataField="Department"></asp:BoundField>
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Export" OnClick="ExportToPDF" runat="server" />
Namespaces
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGridSiriAudit();
}
}
public void BindGridSiriAudit()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(conString))
{
try
{
SqlCommand cmd = new SqlCommand("SELECT * FROM SiriAudit", conn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvSiriAudit.DataSource = ds;
gvSiriAudit.DataBind();
MergeGridviewRows(gvSiriAudit);
}
finally
{
if (conn != null && conn.State == ConnectionState.Open)
{
conn.Close();
}
}
}
}
protected void gvSiriAudit_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gvStaffName = (GridView)e.Row.FindControl("gvStaffName");
string kdkump = gvSiriAudit.DataKeys[e.Row.RowIndex].Values["GROUP"].ToString();
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(conString))
{
try
{
SqlCommand cmd = new SqlCommand("SELECT * FROM StaffName WHERE Id =@kdkump ", conn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@kdkump", SqlDbType.VarChar).Value = kdkump;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvStaffName.DataSource = ds;
gvStaffName.DataBind();
}
finally
{
if (conn != null && conn.State == ConnectionState.Open)
{
conn.Close();
}
}
}
}
}
private void MergeGridviewRows(GridView gridView)
{
for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow gvRow = gridView.Rows[rowIndex];
GridViewRow gvPreviousRow = gridView.Rows[rowIndex + 1];
if (gvRow.Cells[0].Text == gvPreviousRow.Cells[0].Text)
{
if (gvPreviousRow.Cells[0].RowSpan < 2)
{
gvRow.Cells[0].RowSpan = 2;
}
else
{
gvRow.Cells[0].RowSpan = gvPreviousRow.Cells[0].RowSpan + 1;
}
gvPreviousRow.Cells[0].Visible = false;
}
if (gvRow.Cells[0].Text == gvPreviousRow.Cells[0].Text)
{
if (gvPreviousRow.Cells[1].RowSpan < 2)
{
gvRow.Cells[1].RowSpan = 2;
}
else
{
gvRow.Cells[1].RowSpan = gvPreviousRow.Cells[2].RowSpan + 1;
}
gvPreviousRow.Cells[1].Visible = false;
}
if (gvRow.Cells[0].Text == gvPreviousRow.Cells[0].Text)
{
if (gvPreviousRow.Cells[2].RowSpan < 2)
{
gvRow.Cells[2].RowSpan = 2;
}
else
{
gvRow.Cells[2].RowSpan = gvPreviousRow.Cells[2].RowSpan + 1;
}
gvPreviousRow.Cells[2].Visible = false;
}
if (gvRow.Cells[0].Text == gvPreviousRow.Cells[0].Text)
{
if (gvPreviousRow.Cells[3].RowSpan < 2)
{
gvRow.Cells[3].RowSpan = 2;
}
else
{
gvRow.Cells[3].RowSpan = gvPreviousRow.Cells[3].RowSpan + 1;
}
gvPreviousRow.Cells[3].Visible = false;
}
if (gvRow.Cells[0].Text == gvPreviousRow.Cells[0].Text)
{
if (gvPreviousRow.Cells[4].RowSpan < 2)
{
gvRow.Cells[4].RowSpan = 2;
}
else
{
gvRow.Cells[4].RowSpan = gvPreviousRow.Cells[4].RowSpan + 1;
}
gvPreviousRow.Cells[4].Visible = false;
}
if (gvRow.Cells[5].Text == gvPreviousRow.Cells[5].Text)
{
if (gvPreviousRow.Cells[5].RowSpan < 2)
{
gvRow.Cells[5].RowSpan = 2;
}
else
{
gvRow.Cells[5].RowSpan = gvPreviousRow.Cells[5].RowSpan + 1;
}
gvPreviousRow.Cells[5].Visible = false;
}
}
}
protected void gvAuditPlanSearch_PreRender(object sender, EventArgs e)
{
//MergeGridviewRows(gvSiriAudit);
}
protected void ExportToPDF(object sender, EventArgs e)
{
//Set the Size of PDF document.
Rectangle rect = new Rectangle(500, 300);
Document pdfDoc = new Document(rect, 10f, 10f, 10f, 10f);
//Initialize the PDF document object.
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
//Loop through GridView Pages.
for (int i = 0; i < gvSiriAudit.PageCount; i++)
{
//Set the Page Index.
gvSiriAudit.PageIndex = i;
//Hide Page as not needed in PDF.
gvSiriAudit.PagerSettings.Visible = false;
//Populate the GridView with records for the Page Index.
this.BindGridSiriAudit();
//Render the GridView as HTML and add to PDF.
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
gvSiriAudit.RenderControl(hw);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
StringReader sr = new StringReader(sw.ToString());
htmlparser.Parse(sr);
}
}
//Add a new Page to PDF document.
pdfDoc.NewPage();
}
//Close the PDF document.
pdfDoc.Close();
//Download the PDF file.
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}
Screenshot
Output