GridView - no columns

G

Guest

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
 
M

Mark Rae [MVP]

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?
 
G

Guest

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
 
G

Guest

Hi Guy,

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

Regards,
Manish
 
G

Guest

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
 
G

Guest

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
 
G

Guest

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

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top