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. Advertising

  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. Advertising

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. David
    Replies:
    0
    Views:
    334
    David
    May 2, 2004
  2. Jim Brandley

    Title in forms drawn by showModalDialog

    Jim Brandley, Jun 30, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,141
    Jim Brandley
    Jun 30, 2004
  3. Ken Varn
    Replies:
    0
    Views:
    408
    Ken Varn
    Nov 12, 2004
  4. Anjan Bhowmik
    Replies:
    1
    Views:
    509
    Misbah Arefin
    Feb 14, 2008
  5. RB
    Replies:
    2
    Views:
    197
Loading...

Share This Page