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

Discussion in 'ASP .Net Datagrid Control' started by Henrik, Jul 3, 2006.

  1. Henrik

    Henrik Guest

    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
     
    Henrik, Jul 3, 2006
    #1
    1. Advertisements

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

    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>

    "Henrik" <> wrote in message
    news:%...
    > 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
     
    Ken Cox [Microsoft MVP], Jul 6, 2006
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SklNLkgu?=

    Q: datagrid header/footer

    =?Utf-8?B?SklNLkgu?=, Apr 22, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    2,264
    JIM.H.
    Apr 25, 2005
  2. abc
    Replies:
    1
    Views:
    785
    Shaun McDonnell
    Apr 30, 2006
  3. Replies:
    2
    Views:
    890
  4. stephen

    DataGrid Header and Footer Question

    stephen, Aug 15, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    490
    Patrick.O.Ige
    Aug 16, 2006
  5. Anjan Bhowmik
    Replies:
    1
    Views:
    668
    Misbah Arefin
    Feb 14, 2008
  6. Matt

    Footer not in datagrid items list

    Matt, Jun 30, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    356
    Colin Young
    Jul 1, 2003
  7. rosh

    adding button to header, footer or Pager of dataGrid

    rosh, Oct 7, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    408
  8. RB
    Replies:
    2
    Views:
    279
Loading...