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. Problem with GridView Column Count when databinding programmatically?

Problem with GridView Column Count when databinding programmatically?

Scheduled Pinned Locked Moved ASP.NET
helpcsharpdatabasequestion
3 Posts 3 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.
  • K Offline
    K Offline
    kbalias
    wrote on last edited by
    #1

    Hello I am using Visual Web Developer 2010 Express (with C# as code-behind) to develop a website. I am loading data from the database into a DataTable and then I bind the DataTable to GridView with the following code:

    GridView_Report.DataSource = reportsTable;
    GridView_Report.DataBind();

    The GridView displays the data correctly with all the columns and rows. On the Page I also have a button that will download the data in the GridView to a .csv file. However there was an error and when I debugged it turns out that the GridView_Report.Columns.Count says that the GridView has 0 columns. How can this be when the GridView actually shows about 15 columns in the browser? The GridView_Report.Rows.Count is correct with 27 rows. All help will be appreciated. Kobus

    A B 2 Replies Last reply
    0
    • K kbalias

      Hello I am using Visual Web Developer 2010 Express (with C# as code-behind) to develop a website. I am loading data from the database into a DataTable and then I bind the DataTable to GridView with the following code:

      GridView_Report.DataSource = reportsTable;
      GridView_Report.DataBind();

      The GridView displays the data correctly with all the columns and rows. On the Page I also have a button that will download the data in the GridView to a .csv file. However there was an error and when I debugged it turns out that the GridView_Report.Columns.Count says that the GridView has 0 columns. How can this be when the GridView actually shows about 15 columns in the browser? The GridView_Report.Rows.Count is correct with 27 rows. All help will be appreciated. Kobus

      A Offline
      A Offline
      Arun Jacob
      wrote on last edited by
      #2

      From MSDN, "Automatically generated column fields are not added to the Columns collection." Try getting the count from the datasource object instead of gridview.

      Arun Jacob My Technical Blog : Code.NET

      1 Reply Last reply
      0
      • K kbalias

        Hello I am using Visual Web Developer 2010 Express (with C# as code-behind) to develop a website. I am loading data from the database into a DataTable and then I bind the DataTable to GridView with the following code:

        GridView_Report.DataSource = reportsTable;
        GridView_Report.DataBind();

        The GridView displays the data correctly with all the columns and rows. On the Page I also have a button that will download the data in the GridView to a .csv file. However there was an error and when I debugged it turns out that the GridView_Report.Columns.Count says that the GridView has 0 columns. How can this be when the GridView actually shows about 15 columns in the browser? The GridView_Report.Rows.Count is correct with 27 rows. All help will be appreciated. Kobus

        B Offline
        B Offline
        Blue_Boy
        wrote on last edited by
        #3

        You can get Columns from DataSource of GridView control. e.g

        int TotalColumns = ((DataTable)GridView_Report.DataSource).Columns.Count;


        I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post. www.cacttus.com

        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