c# - GridView Column.Count is always 0 after databind with a datatable -


i trying show/hide gridview columns conditionally.

i creating dynamic datatable , binding gridview

later, on post back, checking condition , want show/hide few columns of gridview, column.count alway 0!

the code have below - aspx page

<asp:gridview id="gridview1" runat="server"> </asp:gridview> 

.cs page

 protected void button1_click(object sender, eventargs e) {    datatable adt = new datatable()    adt = createdatatable();   //datatable has 21 columns     gridview1.datasource = adt;    gridview1.databind();     int temp = gridview1.columns.count;   //always 0 } 

not sure wrong code

if set autogeneratedcolumns property true count show 0.

from msdn

the columns property (collection) used store explicitly declared column fields rendered in gridview control. can use columns collection programmatically manage collection of column fields.

in case, didn't declared columns explicitly. can columns count inside , outside of click method if gridview contains @ least 1 row:

cells count in row = columns count.

if(gridview1.rows.count>0)     int temp = gridview1.rows[0].cells.count; 

or can counts datatable inside click method @timschmelter commented:

int temp = adt.columns.count 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -