Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI ,

is there any solution to export selected columns of grid view in excel ,
as i found many blogs and solution about the selected rows but not about columns ,
pls guys let me know for any solution for it .

thanks in advance .
Posted
Updated 16-Sep-15 5:58am
v2

1 solution

Suppose you have grid with ID GridView1 and it contains two columns. And you want to export only first column then find below code:
C#
private void btnExportExcel_Click()
{
    Response.Clear();
    Response.Buffer = true;
  
    Response.AddHeader("content-disposition",
     "attachment;filename=MyData.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
 
    GridView1.AllowPaging = false;
    GridView1.DataBind();
 
    GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
    GridView1.HeaderRow.Cells[0].Style.Add("background-color", "green");
    GridView1.HeaderRow.Cells[1].Style.Add("background-color", "green");
	
    GridView1.HeaderRow.Cells[0].Visible = True;
    GridView1.HeaderRow.Cells[1].Visible = False;
 
    for (int i = 0; i < GridView1.Rows.Count;i++ )
    {
        GridViewRow row = GridView1.Rows[i];
        row.Cells[0].Visible = True;
        row.Cells[1].Visible = False;
        row.BackColor = System.Drawing.Color.White;
        row.Attributes.Add("class", "textmode");
    }

    GridView1.RenderControl(hw);
    string style = @"<style> .textmode { mso-number-format:\@; } </style>";
    Response.Write(style);
    Response.Output.Write(sw.ToString());
    Response.End();
}

Here I have hard coded(True/False) and if you need it dynamic then you ad checkboxes to side of the column and onchange of checkbox store it in viewstate. Later you can fetch checked columns from view state.

http://www.aspsnippets.com/Articles/ASP.Net-GridView-Export-only-selected-columns-to-Excel-Sheet.aspx[^]
 
Share this answer
 
v3
Comments
amitkumar5734 17-Sep-15 10:30am    
not working i have tried this one , any other method to select the columns and export .
[no name] 17-Sep-15 10:36am    
Which line you are getting error and what exception you are getting.
amitkumar5734 17-Sep-15 10:55am    
actually in my project i have a calendar control and on Calendar1_SelectionChanged event , i am binding my grid view , so on page load when i am trying to call if (IsPostBack) { GetCheckBoxStates();} , throws error "object reference not set to an instance of an object "

here i got error ,

private void GetCheckBoxStates()
{
CheckBox chkCol1 = (CheckBox)gd1.HeaderRow.Cells[0].FindControl("chkCol1");
CheckBox chkCol2 = (CheckBox)gd1.HeaderRow.Cells[0].FindControl("chkCol2");



and i know why this is happing but getting null value but i am not able to solve this where i need to call this function because on selection event my grid view is binding and not able to call my grid view on page load .
amitkumar5734 17-Sep-15 11:10am    
hi any answer .
[no name] 17-Sep-15 11:19am    
Did you add checkbox control in HeaderTemplate of Grideview. Please follow the link properly I provided in my answer it will resolve your problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900