This way you can export single GridView row to Excel.
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>GridView Export</title>
<style type="text/css">
body { font-family: Arial; font-size: 10pt; }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
runat="server" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="Customer Id" />
<asp:BoundField DataField="ContactName" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button Text="Export" runat="server" OnClick ="ExportRow" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="GridView2" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" Visible="false"
RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="Customer Id" />
<asp:BoundField DataField="ContactName" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="City" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Namespaces
using System.IO;
using System.Data;
using System.Drawing;
using System.Web.UI;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls;
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
GridView1.DataSource = this.GetData();
GridView1.DataBind();
}
}
private DataTable GetData()
{
string constr = @"Data Source=.\SQL2019;Initial Catalog=Northwind;Integrated Security=true";
using (SqlConnection con = new SqlConnection(constr))
{
string query = "SELECT CustomerID, ContactName, City, Country FROM Customers";
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = this.GetData();
GridView1.DataBind();
}
protected void ExportRow(object sender, EventArgs e)
{
GridViewRow row = ((sender as Button).NamingContainer as GridViewRow);
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("CustomerID"), new DataColumn("ContactName"), new DataColumn("City") });
string customerId = row.Cells[0].Text;
string name = row.Cells[1].Text;
string city = row.Cells[2].Text;
dt.Rows.Add(customerId, name, city);
this.Export(dt);
}
private void Export(DataTable dt)
{
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView2.DataSource = dt;
GridView2.DataBind();
GridView2.Visible = true;
GridView2.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView2.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView2.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
GridView2.RenderControl(hw);
GridView2.Visible = false;
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
//Export Excel to Response.
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
Screenshots
The GridView
Exported Row