Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6872157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:53:33+00:00 2026-05-27T03:53:33+00:00

I have a grid view. Which contains some columns. Suppose, it has 10 columns

  • 0

I have a grid view. Which contains some columns. Suppose, it has 10 columns out of them only 8 columns has header while 2 columns header are empty. Now i am exporting this gridview into excel which contains all 10 columns with 2 columns without name. How can i exclude that 2 empty header columns while exporting GridView into excel. I am using following code for the same:

        protected void btnExportToExcel_Click(object sender, EventArgs e)
        {
            Export("Customers.xls", this.gdvMessages);
        }

        private void Export(string fileName, GridView gv)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
            HttpContext.Current.Response.ContentType = "application/ms-excel";

            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    //  Create a form to contain the grid
                    Table table = new Table();

                    //  add the header row to the table
                    if (gv.HeaderRow != null)
                    {
                        PrepareControlForExport(gv.HeaderRow);
                        table.Rows.Add(gv.HeaderRow);
                    }

                    //  add each of the data rows to the table
                    foreach (GridViewRow row in gv.Rows)
                    {
                        PrepareControlForExport(row);
                        table.Rows.Add(row);
                    }

                    //  add the footer row to the table
                    if (gv.FooterRow != null)
                    {
                        PrepareControlForExport(gv.FooterRow);
                        table.Rows.Add(gv.FooterRow);
                    }

                    //  render the table into the htmlwriter
                    table.RenderControl(htw);

                    //  render the htmlwriter into the response
                    HttpContext.Current.Response.Write(sw.ToString());
                    HttpContext.Current.Response.End();
                }
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:53:33+00:00Added an answer on May 27, 2026 at 3:53 am

    You could just not include those two columns in your HTML table if you don’t need them…. or you could re-write the table when the user press the “Export to Excel” button and not include the columns. You could also have the table plus another table with hidden columns and get the data from it when the user clicks the “Export to Excel” button. Furthermore, you could not have the data shown at all, and when the User loads the page, or click the Button download the data without the columns. Good luck!

    EDITED: For this exclusive scenario, you may want to edit your code in the following way:

     //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    TableRow tr = new TableRow();
    
                    foreach (DataControlFieldHeaderCell c in gv.HeaderRow.Cells)
                    {
                        if (c.Text != " ") {
                            tr.Cells.Add(new TableCell() { Text = c.Text});
                        }
                    }
    
                    PrepareControlForExport(tr);
                    table.Rows.Add(tr);
                }
    

    What that portion of the code does is to loop through each cell in the header row, and if it’s empty then don’t include it. Then add it to the html table. This can be tweaked if wanted so that the rest of the table add a cellspan where these columns belong. If this is not what you need maybe a couple of screenshots with your actual Gridview and the desired output would be ideal.

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a grid view which contains some sequence numbers. Each column needs to
I have a GridView in which each row has a custom view. The grid
i have a grid-view, which contains an asp image-button for deleting row, i want
I have an image in a view which contains a 9x9 grid. I want
i have a grid view which contain images when click on one image it
I have a view with a grid that contains items added to a workstation.
I have a gridview which contains columns vacancy id and vacancy title and a
I am trapped in one problem. I have a page which contains some taxboxes.
I have a telerik grid in an asp.net mvc3 (RAZOR) View. While designing I
I have a asp:GridView which contains a asp:TextBox within a TemplateField. I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.