GridView - no columns

Discussion in 'ASP .Net' started by =?Utf-8?B?Z3V5?=, Oct 18, 2007.

  1. I have a GridView that successfully populates, however when I interrogate its
    Columns collection in code it has 0 entries (there should be in my case 4)
    I need this to change the visible property of some columns.

    Guy
     
    =?Utf-8?B?Z3V5?=, Oct 18, 2007
    #1
    1. Advertising

  2. "guy" <> wrote in message
    news:D...

    >I have a GridView that successfully populates, however when I interrogate
    >its
    > Columns collection in code it has 0 entries (there should be in my case 4)
    > I need this to change the visible property of some columns.


    Are you doing this after the data has been bound?


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
     
    Mark Rae [MVP], Oct 18, 2007
    #2
    1. Advertising

  3. Hi Guy,

    You need to add the columns in the Columns Collection of the GridView in
    order to access them dynamically.

    Dim col As New BoundField
    col.DataField = "Col1"
    col.HeaderText = "header name"
    Me.GridView1.Columns.Add(col)

    or through designer using the smart arrow tag on the GridView and then
    select EditColumns and add the BoundFields in the Selected Columns list.

    Regards,
    Manish

    You can add it through code using

    "guy" wrote:

    > I have a GridView that successfully populates, however when I interrogate its
    > Columns collection in code it has 0 entries (there should be in my case 4)
    > I need this to change the visible property of some columns.
    >
    > Guy
     
    =?Utf-8?B?TWFuaXNo?=, Oct 18, 2007
    #3
  4. Hi Guy,

    You also need to make the AutogeneratedColumns property of the GridView to
    be false to avoid repitition of the data.

    Regards,
    Manish

    "guy" wrote:

    > I have a GridView that successfully populates, however when I interrogate its
    > Columns collection in code it has 0 entries (there should be in my case 4)
    > I need this to change the visible property of some columns.
    >
    > Guy
     
    =?Utf-8?B?TWFuaXNo?=, Oct 18, 2007
    #4
  5. Thanks Manish,
    I need autogenerated columns as the GridView may be displaying data from
    different sources and have different column properties at different times e.g
    column six may be text, and later a checkbox. how can I interrogate the
    column properties for an autogenerated column?

    guy

    "Manish" wrote:

    > Hi Guy,
    >
    > You need to add the columns in the Columns Collection of the GridView in
    > order to access them dynamically.
    >
    > Dim col As New BoundField
    > col.DataField = "Col1"
    > col.HeaderText = "header name"
    > Me.GridView1.Columns.Add(col)
    >
    > or through designer using the smart arrow tag on the GridView and then
    > select EditColumns and add the BoundFields in the Selected Columns list.
    >
    > Regards,
    > Manish
    >
    > You can add it through code using
    >
    > "guy" wrote:
    >
    > > I have a GridView that successfully populates, however when I interrogate its
    > > Columns collection in code it has 0 entries (there should be in my case 4)
    > > I need this to change the visible property of some columns.
    > >
    > > Guy
     
    =?Utf-8?B?Z3V5?=, Oct 18, 2007
    #5
  6. Hi Guy,

    Then you can try accessing the RowCreated and RowDataBound event of the
    GridView to access the items in any of the columns like below:

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
    Response.Write(e.Row.Cells(1).Text)
    End If
    End Sub

    Regards,
    Manish
    "guy" wrote:

    > Thanks Manish,
    > I need autogenerated columns as the GridView may be displaying data from
    > different sources and have different column properties at different times e.g
    > column six may be text, and later a checkbox. how can I interrogate the
    > column properties for an autogenerated column?
    >
    > guy
    >
    > "Manish" wrote:
    >
    > > Hi Guy,
    > >
    > > You need to add the columns in the Columns Collection of the GridView in
    > > order to access them dynamically.
    > >
    > > Dim col As New BoundField
    > > col.DataField = "Col1"
    > > col.HeaderText = "header name"
    > > Me.GridView1.Columns.Add(col)
    > >
    > > or through designer using the smart arrow tag on the GridView and then
    > > select EditColumns and add the BoundFields in the Selected Columns list.
    > >
    > > Regards,
    > > Manish
    > >
    > > You can add it through code using
    > >
    > > "guy" wrote:
    > >
    > > > I have a GridView that successfully populates, however when I interrogate its
    > > > Columns collection in code it has 0 entries (there should be in my case 4)
    > > > I need this to change the visible property of some columns.
    > > >
    > > > Guy
     
    =?Utf-8?B?TWFuaXNo?=, Oct 18, 2007
    #6
  7. Thanks Manish, I have knocked up class to give more control I know changes
    that I ned to make but it works:)

    cheers

    Guy
    ------------------------

    Option Strict On

    Imports Microsoft.VisualBasic

    Public Class AutoGridColumns
    Private _fieldCells As New List(Of DataControlFieldCell)
    Private _view As GridView

    Public Property FieldCells() As List(Of DataControlFieldCell)
    Get
    Return _fieldCells
    End Get
    Set(ByVal value As List(Of DataControlFieldCell))
    _fieldCells = value
    End Set
    End Property

    Public Sub New(ByVal view As GridView)
    Refresh(view)
    End Sub

    Public Sub Refresh(ByVal view As GridView)
    _view = view
    If _view.Rows.Count > 0 Then
    For Each cell As DataControlFieldCell In _view.Rows(0).Controls
    FieldCells.Add(cell)
    Next
    End If
    End Sub

    Public Sub HideColumn(ByVal columnNo As Integer)

    FieldCells(columnNo).Visible = False

    For Each r As GridViewRow In _view.Rows
    r.Controls(columnNo).Visible = FieldCells(columnNo).Visible
    Next
    _view.HeaderRow.Controls(columnNo).Visible =
    FieldCells(columnNo).Visible

    End Sub

    Public Sub ShowColumn(ByVal columnNo As Integer)

    FieldCells(columnNo).Visible = False

    For Each r As GridViewRow In _view.Rows
    r.Controls(columnNo).Visible = FieldCells(columnNo).Visible
    Next
    _view.HeaderRow.Controls(columnNo).Visible =
    FieldCells(columnNo).Visible
    End Sub
    End Class

    ---------------------------

    "Manish" wrote:

    > Hi Guy,
    >
    > Then you can try accessing the RowCreated and RowDataBound event of the
    > GridView to access the items in any of the columns like below:
    >
    > Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    > System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    > If e.Row.RowType = DataControlRowType.DataRow Then
    > Response.Write(e.Row.Cells(1).Text)
    > End If
    > End Sub
    >
    > Regards,
    > Manish
    > "guy" wrote:
    >
    > > Thanks Manish,
    > > I need autogenerated columns as the GridView may be displaying data from
    > > different sources and have different column properties at different times e.g
    > > column six may be text, and later a checkbox. how can I interrogate the
    > > column properties for an autogenerated column?
    > >
    > > guy
    > >
    > > "Manish" wrote:
    > >
    > > > Hi Guy,
    > > >
    > > > You need to add the columns in the Columns Collection of the GridView in
    > > > order to access them dynamically.
    > > >
    > > > Dim col As New BoundField
    > > > col.DataField = "Col1"
    > > > col.HeaderText = "header name"
    > > > Me.GridView1.Columns.Add(col)
    > > >
    > > > or through designer using the smart arrow tag on the GridView and then
    > > > select EditColumns and add the BoundFields in the Selected Columns list.
    > > >
    > > > Regards,
    > > > Manish
    > > >
    > > > You can add it through code using
    > > >
    > > > "guy" wrote:
    > > >
    > > > > I have a GridView that successfully populates, however when I interrogate its
    > > > > Columns collection in code it has 0 entries (there should be in my case 4)
    > > > > I need this to change the visible property of some columns.
    > > > >
    > > > > Guy
     
    =?Utf-8?B?Z3V5?=, Oct 18, 2007
    #7
  8. You can access the Columns collection for autogenerated columns only in
    RowCreated event.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "guy" <> wrote in message
    news:...
    > Thanks Manish,
    > I need autogenerated columns as the GridView may be displaying data from
    > different sources and have different column properties at different times
    > e.g
    > column six may be text, and later a checkbox. how can I interrogate the
    > column properties for an autogenerated column?
    >
    > guy
    >
    > "Manish" wrote:
    >
    >> Hi Guy,
    >>
    >> You need to add the columns in the Columns Collection of the GridView in
    >> order to access them dynamically.
    >>
    >> Dim col As New BoundField
    >> col.DataField = "Col1"
    >> col.HeaderText = "header name"
    >> Me.GridView1.Columns.Add(col)
    >>
    >> or through designer using the smart arrow tag on the GridView and then
    >> select EditColumns and add the BoundFields in the Selected Columns list.
    >>
    >> Regards,
    >> Manish
    >>
    >> You can add it through code using
    >>
    >> "guy" wrote:
    >>
    >> > I have a GridView that successfully populates, however when I
    >> > interrogate its
    >> > Columns collection in code it has 0 entries (there should be in my case
    >> > 4)
    >> > I need this to change the visible property of some columns.
    >> >
    >> > Guy
     
    Eliyahu Goldin, Oct 18, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. helpful sql
    Replies:
    0
    Views:
    845
    helpful sql
    May 19, 2005
  2. Replies:
    5
    Views:
    630
  3. Replies:
    4
    Views:
    959
  4. Jacksm
    Replies:
    2
    Views:
    886
    Paul Chalekian
    Nov 21, 2006
  5. Vinki

    template columns and bound columns in gridview

    Vinki, Apr 25, 2007, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    921
    Vinki
    Apr 25, 2007
Loading...

Share This Page