Any way to reorder how a datagrid is drawn (header/footer/items vs.header/items/footer)?

H

Henrik

Is there any way to reorder how a datagrid is drawn. I have a situation
where it is preferred to have the footer show up before the already
existing data rows. My footer is being used for adding new rows.

Current/default ordering:

Header
Item1
Item2
Item3
Footer

Desired ordering:

Header
Footer (new record entry)
Item1
Item2
Item3
 
K

Ken Cox [Microsoft MVP]

Hi Henrik,

Here's a way to manipulate the footer. See if it works for you?

Ken
Microsoft MVP [ASP.NET]

<%@ page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
If Not IsPostBack Then
Datagrid1.DataSource = CreateDataSource()
Datagrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_ItemDataBound _
(ByVal sender As Object, _
ByVal e As _
System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles Datagrid1.ItemDataBound
' Simulates a move of the footer to the second line
' Ken Cox [MVP] - July 5/06
' Check if this is the footer
If e.Item.ItemType = ListItemType.Footer Then
' Create a new footer item
Dim dgItemFooter As New DataGridItem(0, 0, ListItemType.Footer)
Dim intCount As Integer
Dim tcells As TableCellCollection
Dim fcell As TableCell
' Get a reference to the original footer cells
tcells = e.Item.Cells
' Loop through the original cells
For intCount = 0 To tcells.Count - 1
fcell = New TableCell
' Get the text from the original cell
fcell.Text = tcells(intCount).Text
' Add the cell to the new 'footer'
dgItemFooter.Cells.Add(fcell)
Next
' Insert the new footer into the datagrid
Datagrid1.Controls(0).Controls.AddAt(2, dgItemFooter)
' Remove the original footer
Datagrid1.Controls(0).Controls.Remove(e.Item)
End If
End Sub


Function CreateDataSource() As Data.DataTable
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
dt.Columns.Add(New Data.DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New Data.DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New Data.DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New Data.DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 5
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Move the footer</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:datagrid id="Datagrid1" runat="server" allowpaging="True"
showfooter="True">
</asp:datagrid>
</div>
</form>
</body>
</html>
 

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,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top