dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersJivani
Manoj kumar sahu
Aamir ali
Navneet
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » ASP.NET WebForms

How to export only checked record from gridview to excel?


Posted Date:     Category: ASP.NET WebForms    
Author: Member Level: Diamond    Points: 40


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)





  • Did you like this resource? Share it with your friends and show your love!


    Responses to "How to export only checked record from gridview to excel?"

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

    Feedbacks      

    Post 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to integrate PayPal in ASP.NET using cart option ?
    Previous Resource: What is SCOPE_IDENTITY ? How to use SCOPE_IDENTITY?
    Return to Resources
    Post New Resource
    Category: ASP.NET WebForms


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Export Selected record  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.