Looping Through Controls In DataGrid

J

Jim Heavey

Hello, I am trying to figure out how to get to the "footer" control in a
datagrid. I set up the following logic:

Dim cntrl As Control
Dim cnt As Integer = dgNewMovies.Controls.Count

For Each cntrl In dgNewMovies.Controls
Dim cntrlName As String = cntrl.ID
If TypeOf cntrl Is System.Web.UI.WebControls.DataGridItem Then
Dim item As System.Web.UI.WebControls.DataGridItem = CType(cntrl,
System.Web.UI.WebControls.DataGridItem)
If item.ItemType = ListItemType.Footer Then

End If
End If
Next


I only get a single control... My count (2nd instruction) only has a count
of 1.

I thought that perhaps I needed to look for System.Web.UI.Control but the
way that it is stated is a System.Web.UI.Control.

So how do I iterate through all of the controls on the Datagrid!

Thanks in advance for your assistance!!!
 
K

Ken Cox [Microsoft MVP]

Hi Jim,

The easiest way is to test for the footer in the ItemCreated event. Here's
some code:

<asp:DataGrid id="DataGrid1" runat="server"
ShowFooter="True"></asp:DataGrid>

----


Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_ItemCreated _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemCreated
If e.Item.ItemType = ListItemType.Footer Then
e.Item.Cells(0).Text = Date.Now.ToLongDateString
End If
End Sub

Function CreateDataSource() As ICollection
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dt.Rows.Add(dr)
Next i
Dim dv As New DataView(dt)
Return dv
End Function 'CreateDataSource
 
J

Jim Heavey

I ma trying to write a CustomValidator server edit, so I do not think the
ItemCreated Event will help...will it?
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top