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

  • SEARCH
  • Home
  • 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 7400537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:15:51+00:00 2026-05-29T04:15:51+00:00

I have this code which export data from GridView to csv. It works with

  • 0

I have this code which export data from GridView to csv. It works with other sites but not on this current one I’ve been developing.

The GridView is binded with DataTable in code behind. Following is the event that binds the fetch and bind the data to GridView.

    private void bindGridView()
    {
        //Fetching data from DB goes here

        myTable.Columns.Add("type", typeof(int));
        myTable.Columns.Add("rate", typeof(int));

        foreach (DataRow rows in myTable.Rows)
        {
            if (rows["dst"].ToString() == "1875")
            {
                rows["type"] = 1;
                rows["rate"] = 500;
                rows.AcceptChanges();
            }
            else if (rows["dst"].ToString() == "1876")
            {
                rows["type"] = 0;
                rows["rate"] = 30;
                rows.AcceptChanges();
            }
        }

        gridViewData.DataSource = myTable;
        gridViewData.AllowPaging = true;
        gridViewData.PageSize = 10;
        gridViewData.DataBind();
    }

Following is the button click event to export data from GridView

    protected void btnExportCDR_Click(object sender, EventArgs e)
    {
        if (gridViewData.Rows.Count == 0)
        {
            lblStatus.Text = "Data is empty. Can not export CDR. Please check your filtering dates.";
        }
        else
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=CDRMMCC_" + System.DateTime.Now.ToShortDateString() + ".csv");
            Response.Charset = "";
            Response.ContentType = "application/text";


            bindGridView();
            gridViewData.AllowPaging = false;



            StringBuilder sb = new StringBuilder();

            //I did a trace here, gridViewData.Columns.Count is 0. That's why it got skipped, I think.
            for (int k = 0; k < gridViewData.Columns.Count; k++)
            {
                sb.Append(gridViewData.Columns[k].HeaderText + ",");

            }
            sb.Append("\r\n");
            for (int i = 0; i < gridViewData.Rows.Count; i++)
            {
                for (int k = 0; k < gridViewData.Columns.Count; k++)
                {
                    sb.Append(gridViewData.Rows[i].Cells[k].Text + ",");

                }
                sb.Append("\r\n");
            }
            Response.Output.Write(sb.ToString());
            Response.Flush();
            Response.End();
        }
    }

Please advice.

  • 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-29T04:15:51+00:00Added an answer on May 29, 2026 at 4:15 am

    If you use the AutoGenerateColumns property of the GridView set to true the Columns collection will be empty. The MSDN documentation for this property says:

    “Automatically generated bound column fields are not added to the Columns collection”.

    This is the reason your Columns collection is empty. As Henk Holterman pointed out use your DataTable directly to generate your CSV file.

    An alternative approach would be to set the AutoGenerateColumns property to false and define the Columns explicitly.

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

Sidebar

Related Questions

I have this code which gets WP posts, but it gets all the posts
I have this code (which is way simplified from the real code): public interface
I have this code which compiles and works as expected: class Right {}; class
I have this code which will include template.php file from inside each of these
I've got this code I'm trying to use to export data from Excel to
I currently have some code which iv used to export a table from the
I have this code which is called at an onChange event of an function
I have this code which should create a splash image with either no animation
Problem Hello all! I have this code which takes my jpg image loops through
I am trying to compare two dates. I have this code which I thought

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.