By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,199 Members | 1,714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,199 IT Pros & Developers. It's quick & easy.

Looping Through Controls In DataGrid

P: n/a
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!!!
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

"Jim Heavey" <Ji*******@nospam.com> wrote in message
news:Xn*********************************@207.46.24 8.16...
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!!!

Nov 18 '05 #2

P: n/a
I ma trying to write a CustomValidator server edit, so I do not think the
ItemCreated Event will help...will it?
Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.