.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Export Selected Rows Only

Posted By:Mihir Soni       Posted Date: February 13, 2011    Points: 10    Category: ASP.NET    URL: http://www.dotnetspark.com  

Hello I am going to explain yo how to export only selected rows from gridview


Some time we get unexpected requirements from the client. 

Same problem was happened with me few weeks before They wanted to export some selected row into the excel sheet.

We all are aware of how to export whole gridview but they wanted the different things.

So I have been working on that its not that much difficult task but still its somewhat we need to use our brain how we will use it.:P

And here is code and Code is very understandable and useful.

You can also give as facility in your web application.

First just create your gridview and bind it and add checkbox column to your gridview i am not going in depth.

Put one button called EXPORT and on page and write following code in that.

DataTable dt = new DataTable();
        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);
        GridView GridView1= new GridView()
        GridView1.DataSource = dt;
        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);
I am making datatable,gridview runtime and will bind that gridview with datatable.

Thank you.

Hope this helps you in your projects.

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend