Looping Through Controls In DataGrid

  • Thread starter Thread starter Jim Heavey
  • Start date Start date
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!!!
 
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
 
I ma trying to write a CustomValidator server edit, so I do not think the
ItemCreated Event will help...will it?
 
Back
Top