C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to Export Selected Rows in GridView to ExcelSheet


Posted Date: 09 Jun 2008    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: sriluMember Level: Diamond    
Rating: Points: 10



This code shows how to export selected rows(using checkbox template column to select the records)from the Gridview to Excel Sheet.First add all the selected rows to the datatable and then export that to excel sheet

DataTable dt = new DataTable();
dt.Columns.Add("empid");
dt.Columns.Add("empname");
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = (CheckBox)row.FindControl("CheckBox1");
if (chk.Checked == true)
{
int i = row.RowIndex;
Label lbl = (Label)GridView1.Rows[i].FindControl("Label1");
Label lbl1 = (Label)GridView1.Rows[i].FindControl("Label2");
DataRow dr = dt.NewRow();
dr["empid"] = Convert.ToString(lbl.Text);
dr["empname"] = Convert.ToString(lbl1.Text);
dt.Rows.Add(dr);
}
}
GridView GridView1= new GridView()
GridView1.DataSource = dt;
GridView1.DataBind();
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/ms-excel";
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", "selectedrows"));
Response.Charset = "";
System.IO.StringWriter stringwriter = new StringWriter();
HtmlTextWriter htmlwriter = new HtmlTextWriter(stringwriter);
gd.RenderControl(htmlwriter);
Response.Write(stringwriter.ToString());
Response.End();




Responses

Author: Kapil Dhawan    17 Jun 2008Member Level: Gold   Points : 2
Hello
Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
This code is going to help lots of guys.
Ton Thanks to you
Regards,
Kapil



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: The Process of Uploading and Viewing Photos
Previous Resource: How to prevent Right click on page
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use