Add label to cell next to Pager

A

Andrew Jocelyn

Hi

I'm trying to add a cell in the ItemCreated event which appears next to the
Pager cell and contains a label. At the moment I'm getting a 'Multiple
controls with the same ID' exception. I guess this is do to with the Pager
appearing twice, i.e. top and bottom? What's the best way to get around this
problem please? Here's my code so far:

Private Sub DataGrid_ItemCreated(ByVal sender As System.Object, ByVal e As
DataGridItemEventArgs) Handles DataGrid1.ItemCreated
If e.Item.ItemType = ListItemType.Pager Then
Dim pager As TableCell = CType(e.Item.Controls(0), TableCell)
'Additional Pager Text
Dim dgPagerText As New TableCell
dgPagerText.ID = "dgPagerText"
dgPagerText.HorizontalAlign = HorizontalAlign.Right
'Trace.Write("page stats", PageStats.Text)
dgPagerText.Controls.Add(PageStats)
'Add Table Cell to Pager
e.Item.Controls.AddAt(0, dgPagerText)
End If
End Sub

Many thanks
Andrew
 
K

Ken Cox [Microsoft MVP]

Hi Andrew,

It isn't clear what effect you are trying to achieve, but if it is to simply
add some text before the page numbers, this should give you an idea:

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 _
DataGridItemEventArgs) Handles DataGrid1.ItemCreated
If e.Item.ItemType = ListItemType.Pager Then
Dim pager As TableCell
Dim lblMyText As New Label
lblMyText.Text = "Extra text goes here "
pager = CType(e.Item.Controls(0), TableCell)
pager.Controls.AddAt(0, lblMyText)
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 _
("PercentValue", GetType(Double)))
dt.Columns.Add _
(New DataColumn("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 0.23 * (i + 1)
dr(3) = True
dt.Rows.Add(dr)
Next i
Dim dv As New DataView(dt)
Return dv
End Function 'CreateDataSource


<asp:DataGrid id="DataGrid1" runat="server" ShowFooter="True"
PageSize="2" AllowPaging="True">
<Columns>
<asp:BoundColumn DataField="Percentvalue" ReadOnly="True"
HeaderText="Percentvalue" DataFormatString="{0:p}"></asp:BoundColumn>
</Columns>
<PagerStyle Position="TopAndBottom" PageButtonCount="2"
Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top