Last post Jan 21, 2016 07:33 AM by Weibo Zhang
Jan 21, 2016 05:52 AM|Rameezwaheed|LINK
I have merge the column in gridview based on condition on row created event like below. but while exporting to excel using closedxml it loosing formatting.
protected void gdvRowCreated(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
DataRowView drv = (DataRowView)e.Row.DataItem;
if (drv["ColumnName"].ToString() == string.Empty)
for (int i = 0; i < grdListofAllData.Columns.Count - 1; i++)
e.Row.Cells.ColumnSpan = grdListofAllData.Columns.Count;
How to retain the formatting in gridview i also tried with on Row databount event but issue remain the same.
Jan 21, 2016 07:33 AM|Weibo Zhang|LINK
I think you could first refer to the following article to learn how to merger the GridView cells in the DataBound event.
Then, you could refer to the following code to export the GridView to Excel. I have created a demo based on the above link and the following code, it works on my client. If I move the merging function to the RowCreated, when I click the button to export
the GridView, the “EventArgs” of “RowCreated” method would be empty.
protected void Button1_Click(object sender, EventArgs e)
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataSource = GetData("SELECT ContactName, Country, City FROM Customers GROUP BY Country, City, ContactName");
GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
cell.BackColor = GridView1.HeaderStyle.BackColor;
public override void VerifyRenderingInServerForm(Control control)