How to export only checked record from gridview to excel?


In this article I am going to explain about how to export only selected checkbox row record from gridview to excel in ASP.NET. This concept is may be used in your project various situation.

Description :


Mostly we are display large number of records in the grid view. At that time we want only particular record details can export to excel using this resource technique you can export that selected row all details to excel.

Client side


I have placed one gridview with checkbox column and provide one button export to excel.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Gridview Export</title>
<script language="javascript" type="text/javascript">
function SelectAll(id) {
var frm = document.forms[0];
for (i = 0; i < frm.elements.length; i++) {
if (frm.elements[i].type == "checkbox") {
frm.elements[i].checked = document.getElementById(id).checked;
}
}
}
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<b>Export to Gridview only selected checkbox Record</b><br /><br />
<asp:GridView ID="GridView1" runat="server" DataKeyNames="eno" AutoGenerateColumns="false"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
<%-- if we no need Check box in header then hide it below Header Template and also comment server side row data bound code--%>
<HeaderTemplate>
<asp:CheckBox ID="SelectAll" runat="server" />
</HeaderTemplate>
<EditItemTemplate>
<asp:CheckBox ID="Chkbx" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="eno" HeaderText="Employee Name" />
<asp:BoundField DataField="empname" HeaderText="Employee Name" />
<asp:BoundField DataField="sal" HeaderText="Employee Name" />
</Columns>
</asp:GridView><br />
<asp:Button ID="Button2" runat="server" Text="Export checked only" OnClick="Button2_Click" />
</div>
</form>
</body>
</html>

Server side


Here I get only selected record cells details and export to excel

using System.Data;

public partial class _Default : System.Web.UI.Page
{
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
DataRow dr;
DataRow dr1;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}

void BindGrid()
{
dt.Columns.Add("eno");
dt.Columns.Add("empname");
dt.Columns.Add("sal");
dr = dt.NewRow();
dr["eno"] = "101";
dr["empname"] = "Ravindran";
dr["sal"] = "45000";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["eno"] = "102";
dr["empname"] = "James";
dr["sal"] = "35000";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["eno"] = "103";
dr["empname"] = "Mike";
dr["sal"] = "20000";
dt.Rows.Add(dr);

if (dt.Rows.Count > 0)
{
Session["source_table"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
void ExportExcel(DataTable dt)
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("ddMMyyyy") + ".xls");
Response.ContentType = "application/ms-excel";
string tab = "";
foreach (DataColumn dc in dt.Columns)
{
Response.Write(tab + dc.ColumnName);
tab = "\t";
}
Response.Write("\n");

int i;
foreach (DataRow dr in dt.Rows)
{
tab = "";
for (i = 0; i < dt.Columns.Count; i++)
{
Response.Write(tab + dr[i].ToString());
tab = "\t";
}
Response.Write("\n");
}
Response.End();

}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowType == DataControlRowType.Header))
{
((CheckBox)e.Row.FindControl("SelectAll")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("SelectAll")).ClientID + "')");
}
}
//Export only selected record
protected void Button2_Click(object sender, EventArgs e)
{
int k = 0;
//Checkther whether atleast one check box is selected or not
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
GridViewRow row = GridView1.Rows[i];
CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox1");
if (Ckbox.Checked == true)
{
k++;
}
}

if (k == 0)
{
Page.RegisterStartupScript("Alert Message", "<script language='javascript'>alert('Select atleast one Check box further to proceed');</script>");
return;
}
dt1.Columns.Add("eno");
dt1.Columns.Add("empname");
dt1.Columns.Add("sal");
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
GridViewRow row = GridView1.Rows[i];
CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox1");
if (Ckbox.Checked == true)
{
dr1 = dt1.NewRow();
dr1["eno"] = GridView1.Rows[i].Cells[1].Text;
dr1["empname"] = GridView1.Rows[i].Cells[2].Text;
dr1["sal"] = GridView1.Rows[i].Cells[3].Text;
dt1.Rows.Add(dr1);
}
}
ExportExcel(dt1);
}
}

Output


output

Source code:

Client Side: ASP.NET
Code Behind: C#

Conclusion

I hope this article is help you to know about export to excel only selected record.


Attachments

  • Source _code (44019-02635-Source-code.rar)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: