Problem with GridView Column Count when databinding programmatically?
-
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? TheGridView_Report.Rows.Count
is correct with 27 rows. All help will be appreciated. Kobus -
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? TheGridView_Report.Rows.Count
is correct with 27 rows. All help will be appreciated. KobusFrom 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
-
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? TheGridView_Report.Rows.Count
is correct with 27 rows. All help will be appreciated. KobusYou 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