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

前端 未结 3 1279
眼角桃花
眼角桃花 2021-01-14 01:37

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

3条回答
  •  感动是毒
    2021-01-14 02:39

    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
    

提交回复
热议问题