Hello again-
One of the columns in my datalist contains an integer value. There are times in my list that I need to show the total for a number of rows.
For example,
1
2
2
--
5
2
1
3
--
6
and so on.
Here's my code:
Code:
Public CaseTotal As Integer = 0
.
.
Public Sub dlItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim CenterLabel, lblSep, Cases, Total As Label
Select Case e.Item.ItemType
Case ListItemType.Item
CenterLabel = CType(e.Item.FindControl("lblCenter"), Label)
lblSep = CType(e.Item.FindControl("lblSeparate"), Label)
Cases = CType(e.Item.FindControl("lblCases"), Label)
Total = CType(e.Item.FindControl("lblTotal"), Label)
Case ListItemType.AlternatingItem
CenterLabel = CType(e.Item.FindControl("lblAltCenter"), Label)
lblSep = CType(e.Item.FindControl("lblAltSeparate"), Label)
Cases = CType(e.Item.FindControl("lblAltCases"), Label)
Total = CType(e.Item.FindControl("lblAltTotal"), Label)
End Select
CaseTotal += CInt(Cases.Text)
Total.Text = "Total: " & FormatNumber(CaseTotal, 0, , , TriState.True)
If curCenter = CenterLabel.Text Then
CenterLabel.Visible = False
lblSep.Visible = False
Total.Visible = False
Else
'new Center
CenterLabel.Visible = True
lblSep.Visible = True
Total.Visible = True
CaseTotal = 0
End If
curCenter = CenterLabel.Text
End If
End Sub
The idea is to total up the value in lblCases to display when there is a new center.
The line
Code:
CaseTotal += CInt(Cases.Text)
gives an error, "InvalidCastException: Cast from string "" to type 'Integer' is not valid." That is, Cases.Text is empty.
What gives?
- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.