I am trying to show/hide GridView columns conditionally.
I am creating a dynamic DataTable and then Binding it to the GridView
Later, on a post back, I am ch
If you set the autogeneratedcolumns property to true then the count will always show 0.
From MSDN
The Columns property (collection) is used to store all the explicitly declared column fields that get rendered in the GridView control. You can also use the Columns collection to programmatically manage the collection of column fields.
In your case, you didn't declared your columns explicitly. So you can get columns count like this inside and outside of the Click method if GridView contains at least one row:
Cells Count in a Row = Columns Count.
if(GridView1.Rows.Count>0)
int temp = GridView1.Rows[0].Cells.Count;
Or you can get counts from DataTable inside Click method like that as @TimSchmelter commented:
int temp = aDT.Columns.Count