Find footer labels in DataGrid

T

tshad

How do I find (and set) a couple of labels in the Footer after a DataGrid is
filled?

I have a bunch of DataGrids that get displayed nested inside a DataList.

The datagrid looks like:

*******************************************************************************
<asp:DataGrid visible="False"
border=1
id="DataGrid1"
runat="server"
Width="400px"
Height="79px"
AutoGenerateColumns="False"
GridLines="None"
ShowFooter="true"
onItemDataBound="DataGrid1_ItemDataBound"
OnItemCommand="DataInsert"
onEditCommand="DataEdit"
onCancelCommand="DataCancel"
onUpdateCommand="DataUpdate">
<AlternatingItemStyle BorderWidth="0px" BorderStyle="None"
BorderColor="White" BackColor="linen"></AlternatingItemStyle>
<HeaderStyle Font-Bold="True" BackColor="#6699cc"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Answer">
<ItemTemplate>
<asp:Label id="lblAnswer" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Answer") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAnswerFooter" runat="server"></asp:TextBox>
</FooterTemplate>
<EditItemTemplate>
<asp:textbox id="txtAnswer" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Answer") %>'>
</asp:textbox>
</EditItemTemplate>
</asp:templateColumn>
<asp:TemplateColumn >
<ItemTemplate>
<asp:Label id="lblPositionID" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.PositionID") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label id="lblPositionIDFooter" runat="server">
</asp:Label>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn >
<ItemTemplate>
<asp:Label id="lblQuestionUnique" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.QuestionUnique") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label id="lblQuestionUniqueFooter" runat="server">
</asp:Label>
</FooterTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="AnswerUnique" visible="false" ReadOnly="true"
HeaderText="AnswerUnique"></asp:BoundColumn>
<asp:EditCommandColumn EditText="<img src='..\images\Edit.png'border='0'
id='textbox1'>" visible="true"
ButtonType="LinkButton"
UpdateText="<img src='..\images\update.png' border='0'>"
CancelText="<img src='..\images\Cancel.png' border='0'>" />
<asp:TemplateColumn >
<FooterTemplate>
<asp:LinkButton CommandName="Insert" Text="<img
src='..\images\Add.gif'border='0'>" ID="btnAdd" Runat="server" />
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton CommandName="Delete" Text="<img
src='..\images\Delete.gif'border='0'>" ID="btnDel" Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
***************************************************************************************

I have the DataGrid by doing something like:

for each oItem as DataListItem in DataList1.Items
Dim oGrid as DataGrid = CType(oItem.FindControl("DataGrid1"),DataGrid)

How would I use oGrid to find and set "lblPositionIDFooter" and
"lblQuestionUniqueFooter"?

I need this as I need a place to put the PositionID and QuestionUnique as
they are keys I need to use in my Insert statement. I am inserting rows
from the Footer and need to get these variables from some where, so this
seems to be the most logical place to store them.

Thanks,

Tom.
 
S

Sonu Kapoor

Try: e.Item.FindControl("lblPositionIDFooter)

Sonu

-----Original Message-----
How do I find (and set) a couple of labels in the Footer after a DataGrid is
filled?

I have a bunch of DataGrids that get displayed nested inside a DataList.

The datagrid looks like:

********************************************************** *********************
<asp:DataGrid visible="False"
border=1
id="DataGrid1"
runat="server"
Width="400px"
Height="79px"
AutoGenerateColumns="False"
GridLines="None"
ShowFooter="true"
onItemDataBound="DataGrid1_ItemDataBound"
OnItemCommand="DataInsert"
onEditCommand="DataEdit"
onCancelCommand="DataCancel"
onUpdateCommand="DataUpdate">
<AlternatingItemStyle BorderWidth="0px" BorderStyle="None"
BorderColor="White"
BackColor="linen"> said:
<HeaderStyle Font-Bold="True"
BackColor="#6699cc"> said:
<Columns>
<asp:TemplateColumn HeaderText="Answer">
<ItemTemplate>
<asp:Label id="lblAnswer" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Answer") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAnswerFooter"
runat="server"> said:
</FooterTemplate>
<EditItemTemplate>
<asp:textbox id="txtAnswer" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Answer") %>'>
</asp:textbox>
</EditItemTemplate>
</asp:templateColumn>
<asp:TemplateColumn >
<ItemTemplate>
<asp:Label id="lblPositionID" runat="server" Text='<% #
DataBinder.Eval(Container, "DataItem.PositionID") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label id="lblPositionIDFooter" runat="server">
</asp:Label>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn >
<ItemTemplate>
<asp:Label id="lblQuestionUnique" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.QuestionUnique") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label id="lblQuestionUniqueFooter" runat="server">
</asp:Label>
</FooterTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="AnswerUnique"
visible="false" ReadOnly="true"
 
S

Scott Allen

Hi tshad:

You might want to try during the ItemDataBound event instead (check
for e.Item.ItemType == ListItemType.Footer).

If you need to wait until afterwards, then you can index into the
Controls collection of the grid. (The Items collection might look
tempting at first, but it only contains DataGridItems bound to
records, the header and footer items appear only in the Controls
collection). The footer item should be in the last element of the
Controls array.

I have some examples about 1/2 through this article:
http://www.odetocode.com/Articles/116.aspx

HTH,
 
T

tshad

Sonu Kapoor said:
Try: e.Item.FindControl("lblPositionIDFooter)

I can't use e.item as the object is a datalist that has a datagrid in it. I
then find the oGrid from that.

So I tried:

Dim fPositionID as Label = oGrid.Item.FindControl("lblPositionIDFooter")

and got the following error message:
************************************************
Compiler Error Message: BC30456: 'Item' is not a member of
'System.Web.UI.WebControls.DataGrid'.

Source Error:

Line 225: Dim oCollapse as ImageButton =
CType(DataList1.Items(DataList1.SelectedIndex).FindControl("CollapseButton")
,ImageButton)
Line 226:
Line 227: Dim fPositionID as Label =
oGrid.Item.FindControl("lblPositionIDFooter")
*****************************************************************

Tom.
 
T

tshad

Scott Allen said:
Hi tshad:

You might want to try during the ItemDataBound event instead (check
for e.Item.ItemType == ListItemType.Footer).

If you need to wait until afterwards, then you can index into the
Controls collection of the grid. (The Items collection might look
tempting at first, but it only contains DataGridItems bound to
records, the header and footer items appear only in the Controls
collection). The footer item should be in the last element of the
Controls array.

I have some examples about 1/2 through this article:
http://www.odetocode.com/Articles/116.aspx

I tried some of what you had and here is what I came up with.

The routine where I need to get access to the label in the footer is:

**********************************************************************
Sub OnSelectIndexChanged(sender as Object,e as DataListCommandEventArgs)
if e.CommandName = "Select" then
DataList1.SelectedIndex = e.Item.ItemIndex
end if

Dim oGrid as DataGrid =
CType(DataList1.Items(DataList1.SelectedIndex).FindControl("DataGrid1"),Data
Grid)

Dim lblPositionIDF as Label =
oGrid.Controls(0).Controls(0).FindControl("lblPositionIDFooter")
**********************************************************************

Gives me the following error:

Specified argument was out of the range of valid values. Parameter name:
index

The DataGrid footer section looks like:

**********************************************************************
<asp:TemplateColumn visible="true">
<ItemTemplate>
<asp:Label id="lblPositionID" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.PositionID") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label id="lblPositionIDFooter" runat="server">
</asp:Label>
</FooterTemplate>
</asp:TemplateColumn>
**********************************************************************

This doesn't seem to be it, unless I am missing something (and I probably
am).

Thanks,

Tom.
 
G

Guest

tshad,

After a week or two of trying to solve a similar issue, I stumbles upon your
post.

Alleluia! I believe I am on my way to solving my problem. I'll post back in
a couple of hours and let you know if this really was it.

I have a datagrid with 2 nested datagrids. Here is a screenshot.
http://www.smartbizniz.com/ee/SBS_VarianceNarrative_EE_Sample.pdf

I am using the footers of the nested datagrids to add new records. It was
easy for the users to push the Add link to add records one by one. They got
tired of that and wanted to add new records a page at a time.

I could loop through the records in the master datagrid and get at the
nested datagrid but I couldn't figure out how to get the values in the nested
datagrid's footer. Now I have it:

Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnTest.Click

Dim strText As String

For Each item As DataGridItem In DataGrid1.Items

Dim dgTemp As DataGrid = CType(item.FindControl("DataGrid_PTD"),
DataGrid)
Dim txt As TextBox =
CType(dgTemp.Controls(0).Controls(1).FindControl("txtPTDsNarrativeAdd"),
TextBox)
If txt Is Nothing Then
strText = ""
Else
strText = txt.Text
End If

'Now add a new record to the dataset
Next

End Sub

Have you been able to solve your problem?
 
G

Guest

tshad:

This was it!

It took a little while for me to realize that the index for the footer
control changes depending on how many records are in the datagrid. However,
in my case it is always the last control.

Hope you were able to solve your problem.

Here is my code:

Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnTest.Click

Dim strNarrativePTD As String
Dim strNarrativeYTD As String
Dim intFooterPTD As Integer
Dim intFooterYTD As Integer
Dim intNarrativeType As Int16
Dim intSortOrder As Integer = 1 'Sort order has not been implemented yet

For Each item As DataGridItem In DataGrid1.Items

Dim dgPTD As DataGrid = CType(item.FindControl("DataGrid_PTD"),
DataGrid)
Dim dgYTD As DataGrid = CType(item.FindControl("DataGrid_YTD"),
DataGrid)

'The Footer section is the last control in the nested datagrid. We
will use this index later.
intFooterPTD = dgPTD.Controls(0).Controls.Count - 1
intFooterYTD = dgYTD.Controls(0).Controls.Count - 1

'First check the PTD Comments
Dim txtPTD As TextBox =
CType(dgPTD.Controls(0).Controls(intFooterPTD).FindControl("txtPTDsNarrativeAdd"), TextBox)
Dim lblPTD As Label =
CType(dgPTD.Controls(0).Controls(intFooterPTD).FindControl("lblhAcctPTDAdd"),
Label)
If txtPTD Is Nothing Then
strNarrativePTD = ""
Else
strNarrativePTD = txtPTD.Text
strHAcct = lblPTD.Text
intNarrativeType = 1
End If

'If there is something in the textbox, add the record.
If Len(strNarrativePTD) > 0 Then
NarrativesAdd(intNarrativeType, strNarrativePTD, intSortOrder)
End If

'Second check the YTD Comments
Dim txtYTD As TextBox =
CType(dgYTD.Controls(0).Controls(intFooterYTD).FindControl("txtYTDsNarrativeAdd"), TextBox)
Dim lblYTD As Label =
CType(dgYTD.Controls(0).Controls(intFooterYTD).FindControl("lblhAcctYTDAdd"),
Label)
If txtYTD Is Nothing Then
strNarrativeYTD = ""
Else
strNarrativeYTD = txtYTD.Text
strHAcct = lblYTD.Text
intNarrativeType = 12
End If

'If there is something in the textbox, add the record.
If Len(strNarrativeYTD) > 0 Then
NarrativesAdd(intNarrativeType, strNarrativeYTD, intSortOrder)
End If

Next

LoadGrid()

End Sub
 
T

tshad

John Desautels said:
tshad:

This was it!

It took a little while for me to realize that the index for the footer
control changes depending on how many records are in the datagrid.
However,
in my case it is always the last control.

Hope you were able to solve your problem.

Actually,

I did. But not the way I wanted to.

The problem was I have multiple grids inside of a datalist. Each grid was a
different set up records differentiated by a unique number. My problem is
finding out what the unique number was.

I figured I would just look through the datalist until I find the datagrid
that matched the name of the one I was using. I know that the table ID's
would be different when the page was rendered. What I didn't realize that
in ASP, before it is rendered, all the ID's are identical. So that didn't
work.

What I ended up doing was puting the unique number in a hidden column on the
footer as the table was being populated. Then when I did the insert I would
already be pointing at the Footer and I could get the data easily at that
point.

What I haven't been able to do was figure out how to find out which Datalist
row (the parent) is equated with the Datagrid I am working with. I assume I
will have the same problem - same name.

Thanks,

Tom.
 

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,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top