Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add a footer with a link to the datagrid 1

Status
Not open for further replies.

Ankor

Programmer
Mar 21, 2002
144
US
I'd like to add a footer to the datagrid, span all columns in it, and then add a link that would lead to the top of the page. Is it possible to do that? Thank you.
 
It's a good one, but for whatever reason I get an error when I try to RemoveAt(...) more than 8 columns. My code looks like this:

Sub SpanFooter(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Footer Then
e.Item.Cells(0).ColumnSpan = 16
e.Item.Cells(0).Text = "Back to Top"
e.Item.Cells.RemoveAt(1)
e.Item.Cells.RemoveAt(2)
e.Item.Cells.RemoveAt(3)
e.Item.Cells.RemoveAt(4)
e.Item.Cells.RemoveAt(5)
e.Item.Cells.RemoveAt(6)
e.Item.Cells.RemoveAt(7)
e.Item.Cells.RemoveAt(8)
End If
End Sub

If I add e.Item.Cells.RemoveAt(9), I will get an error.
 
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
 
Sub SpanFooter(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Footer Then
e.Item.Cells(0).ColumnSpan = 16
e.Item.Cells(0).Text = "Back to Top"
e.Item.Cells.RemoveAt(1)
e.Item.Cells.RemoveAt(2)
e.Item.Cells.RemoveAt(3)
e.Item.Cells.RemoveAt(4)
e.Item.Cells.RemoveAt(5)
e.Item.Cells.RemoveAt(6)
e.Item.Cells.RemoveAt(7)
e.Item.Cells.RemoveAt(8)
e.Item.Cells.RemoveAt(9)
End If
End Sub
 
I have 16 fields in the table. I can remove first 8. What will be the reason why I cannot remove 9th or 10th?
 
You were right, cell count returns 8, but now I cannot understand why it does it in the 16 field datagrid. Is it a problem with data binding?
 
here's your problem. each time you remove a cell your decrement the cell count by 1. so by the time you are half way through your cells the index doesn't exist.

instead start at index 15 and move back to 1.

Sub SpanFooter(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Footer Then
e.Item.Cells.RemoveAt(15)
e.Item.Cells.RemoveAt(14)
e.Item.Cells.RemoveAt(13)
e.Item.Cells.RemoveAt(12)
e.Item.Cells.RemoveAt(11)
e.Item.Cells.RemoveAt(10)
e.Item.Cells.RemoveAt(9)
e.Item.Cells.RemoveAt(8)
e.Item.Cells.RemoveAt(7)
e.Item.Cells.RemoveAt(6)
e.Item.Cells.RemoveAt(5)
e.Item.Cells.RemoveAt(4)
e.Item.Cells.RemoveAt(3)
e.Item.Cells.RemoveAt(2)
e.Item.Cells.RemoveAt(1)
e.Item.Cells(0).ColumnSpan = 16
e.Item.Cells(0).Text = "Back to Top"
End If
End Sub


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
That's one of the reasons why you should try not to use indexes when deleting anything. Jason's answer should work but a safer option would be to use an enumerator. In this case I think you would be able to get an enumerator of the cells collection e.g.
Code:
        Dim i As IEnumerator = e.Item.Cells.GetEnumerator
        While i.MoveNext

        End While


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
I agree with ca8msm. this should be the preferred method as it's generic and isn't dependent on the number of objects.

to simplify your foot code above
Code:
Sub SpanFooter(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
   If e.Item.ItemType = ListItemType.Footer Then
     e.Item.Cells.Clear()
     TableCell cell = new TableCell()
     cell.ColumnSpan = [GridViewName].Columns.Count
     cell.Text = "Back to Top"
     e.Items.Cells.Add(cell);
  End If
End Sub

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top