Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. export gridview data to CSV file

export gridview data to CSV file

Scheduled Pinned Locked Moved ASP.NET
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Praveen Kandari
    wrote on last edited by
    #1

    Not able to download gridview content. Downloaded CSV file is empty. my code on button click is:-

    public override void VerifyRenderingInServerForm(Control control)
    {
    }
    protected void btnCSV_Click(object sender, ImageClickEventArgs e)
    {
    Response.Content="";
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "attachment.csv"));
    Response.ContentType = "application/text";(also tried vnd.csv)
    gv1.AllowPaging = false;
    gv1.DataBind();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < gv1.Columns.Count; i++)
    {

    sb.Append(gv1.Columns[i].HeaderText + ',');
    }

    sb.Append("\n");
    for (int j = 0; j < gv1.Rows.Count; j++)
    {
    for (int k = 0; k < gv1.Columns.Count; k++)
    {

    sb.Append(gv1.Rows[j].Cells[k].Text + ',');
    }

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

    Z 1 Reply Last reply
    0
    • P Praveen Kandari

      Not able to download gridview content. Downloaded CSV file is empty. my code on button click is:-

      public override void VerifyRenderingInServerForm(Control control)
      {
      }
      protected void btnCSV_Click(object sender, ImageClickEventArgs e)
      {
      Response.Content="";
      Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "attachment.csv"));
      Response.ContentType = "application/text";(also tried vnd.csv)
      gv1.AllowPaging = false;
      gv1.DataBind();
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < gv1.Columns.Count; i++)
      {

      sb.Append(gv1.Columns[i].HeaderText + ',');
      }

      sb.Append("\n");
      for (int j = 0; j < gv1.Rows.Count; j++)
      {
      for (int k = 0; k < gv1.Columns.Count; k++)
      {

      sb.Append(gv1.Rows[j].Cells[k].Text + ',');
      }

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

      Z Offline
      Z Offline
      ZurdoDev
      wrote on last edited by
      #2

      Debug it and make sure sb.ToString() actually has something.

      There are only 10 types of people in the world, those who understand binary and those who don't.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups