Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2007

Tip: Looking for answers? Try searching our database.

GridView - no columns

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
guy - 18 Oct 2007 10:33 GMT
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
Mark Rae [MVP] - 18 Oct 2007 10:56 GMT
>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?

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Manish - 18 Oct 2007 10:59 GMT
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

> 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
guy - 18 Oct 2007 11:10 GMT
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

> Hi Guy,
>
[quoted text clipped - 19 lines]
> >
> > Guy
Manish - 18 Oct 2007 13:13 GMT
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

> Thanks Manish,
> I need autogenerated columns as the GridView may be displaying data from
[quoted text clipped - 27 lines]
> > >
> > > Guy
guy - 18 Oct 2007 14:05 GMT
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

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

> Hi Guy,
>
[quoted text clipped - 42 lines]
> > > >
> > > > Guy
Eliyahu Goldin - 18 Oct 2007 14:05 GMT
You can access the Columns collection for autogenerated columns only in
RowCreated event.

Signature

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

> Thanks Manish,
> I need autogenerated columns as the GridView may be displaying data from
[quoted text clipped - 30 lines]
>> >
>> > Guy
Manish - 18 Oct 2007 11:02 GMT
Hi Guy,

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

Regards,
Manish

> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.