Add additional row into header?

Discussion in 'ASP .Net Datagrid Control' started by SK, Feb 17, 2005.

  1. SK

    SK Guest

    Hello,

    does anybody know how I can add an additional row above
    the datagrid header?
    SK, Feb 17, 2005
    #1
    1. Advertising

  2. Here's some code to show two headers:

    <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False"
    ShowHeader="False">
    <Columns>
    <asp:BoundColumn DataField="Subject"></asp:BoundColumn>
    <asp:BoundColumn DataField="Day"></asp:BoundColumn>
    </Columns>
    </asp:DataGrid>


    Dim dt As DataTable
    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    DataGrid1.ShowHeader = True
    DataGrid1.DataSource = CreateDataSource()
    DataGrid1.DataBind()
    End Sub
    Private Sub DataGrid1_ItemDataBound _
    (ByVal sender As Object, _
    ByVal e As _
    System.Web.UI.WebControls.DataGridItemEventArgs) _
    Handles DataGrid1.ItemDataBound
    ' Add a second header with a flag button in it
    ' by Ken Cox Microsoft MVP [ASP.NET]
    If e.Item.ItemType = ListItemType.Header Then
    ' Get the collection of cells from the grid
    Dim tcells As TableCellCollection
    ' Create an imagebutton
    Dim imgBtn As New ImageButton
    ' Assign the URL
    imgBtn.ImageUrl = "http://www.gc.ca/images/flag.gif"
    ' Get the collection of existing cells so we can get a count
    tcells = e.Item.Cells
    ' Create a new cell
    Dim fcell As New TableCell
    ' Add the image button to the new table cell
    fcell.Controls.Add(imgBtn)
    ' Span the cell to however many columns there are
    fcell.ColumnSpan = tcells.Count
    ' Create a new header object
    Dim dgItemHeader As New DataGridItem _
    (0, 0, ListItemType.Header)
    ' Add the cell to the header
    dgItemHeader.Cells.Add(fcell)
    dgItemHeader.Visible = True
    ' Add the header to the datagrid
    DataGrid1.Controls(0).Controls.Add(dgItemHeader)
    End If
    End Sub
    Function CreateDataSource() As ICollection
    ' Create sample data for the DataList control.
    dt = New DataTable
    Dim dr As DataRow

    ' Define the columns of the table.
    dt.Columns.Add(New DataColumn("Student", GetType(String)))
    dt.Columns.Add(New DataColumn("Subject", GetType(String)))
    dt.Columns.Add(New DataColumn("Day", GetType(String)))

    ' Populate the table with sample values.
    dr = dt.NewRow
    dr(0) = "Ben"
    dr(1) = "English"
    dr(2) = "Thursday"
    dt.Rows.Add(dr)
    dr = dt.NewRow
    dr(0) = "Ben"
    dr(1) = "Geology"
    dr(2) = "Monday"
    dt.Rows.Add(dr)
    dr = dt.NewRow
    dr(0) = "Ben"
    dr(1) = "Physics"
    dr(2) = "Tuesday"
    dt.Rows.Add(dr)
    Dim dv As DataView = New DataView(dt)
    Return dv
    End Function

    "SK" <> wrote in message
    news:297e01c51505$d71cb610$...
    > Hello,
    >
    > does anybody know how I can add an additional row above
    > the datagrid header?
    Ken Cox [Microsoft MVP], Feb 17, 2005
    #2
    1. Advertising

  3. SK

    Saravana Guest

    Saravana, Feb 17, 2005
    #3
  4. SK

    SK Guest

    Many thanks Ken,

    how about if I want to add an additional row beneath
    each exising row?

    Any Idea how I can do that? I tried it, but it showed me
    that the message:

    Server Error in '/intranet' Application.

    Thanks

    >-----Original Message-----
    >Here's some code to show two headers:
    >
    > <asp:DataGrid id="DataGrid1" runat="server"

    AutoGenerateColumns="False"
    >ShowHeader="False">
    > <Columns>
    > <asp:BoundColumn

    DataField="Subject"></asp:BoundColumn>
    > <asp:BoundColumn DataField="Day"></asp:BoundColumn>
    > </Columns>
    > </asp:DataGrid>
    >
    >
    > Dim dt As DataTable
    > Private Sub Page_Load _
    > (ByVal sender As System.Object, _
    > ByVal e As System.EventArgs) _
    > Handles MyBase.Load
    > DataGrid1.ShowHeader = True
    > DataGrid1.DataSource = CreateDataSource()
    > DataGrid1.DataBind()
    > End Sub
    > Private Sub DataGrid1_ItemDataBound _
    > (ByVal sender As Object, _
    > ByVal e As _
    > System.Web.UI.WebControls.DataGridItemEventArgs) _
    > Handles DataGrid1.ItemDataBound
    > ' Add a second header with a flag button in it
    > ' by Ken Cox Microsoft MVP [ASP.NET]
    > If e.Item.ItemType = ListItemType.Header Then
    > ' Get the collection of cells from the grid
    > Dim tcells As TableCellCollection
    > ' Create an imagebutton
    > Dim imgBtn As New ImageButton
    > ' Assign the URL
    > imgBtn.ImageUrl

    = "http://www.gc.ca/images/flag.gif"
    > ' Get the collection of existing cells so we

    can get a count
    > tcells = e.Item.Cells
    > ' Create a new cell
    > Dim fcell As New TableCell
    > ' Add the image button to the new table cell
    > fcell.Controls.Add(imgBtn)
    > ' Span the cell to however many columns there

    are
    > fcell.ColumnSpan = tcells.Count
    > ' Create a new header object
    > Dim dgItemHeader As New DataGridItem _
    > (0, 0, ListItemType.Header)
    > ' Add the cell to the header
    > dgItemHeader.Cells.Add(fcell)
    > dgItemHeader.Visible = True
    > ' Add the header to the datagrid
    > DataGrid1.Controls(0).Controls.Add

    (dgItemHeader)
    > End If
    > End Sub
    > Function CreateDataSource() As ICollection
    > ' Create sample data for the DataList control.
    > dt = New DataTable
    > Dim dr As DataRow
    >
    > ' Define the columns of the table.
    > dt.Columns.Add(New DataColumn("Student", GetType

    (String)))
    > dt.Columns.Add(New DataColumn("Subject", GetType

    (String)))
    > dt.Columns.Add(New DataColumn("Day", GetType

    (String)))
    >
    > ' Populate the table with sample values.
    > dr = dt.NewRow
    > dr(0) = "Ben"
    > dr(1) = "English"
    > dr(2) = "Thursday"
    > dt.Rows.Add(dr)
    > dr = dt.NewRow
    > dr(0) = "Ben"
    > dr(1) = "Geology"
    > dr(2) = "Monday"
    > dt.Rows.Add(dr)
    > dr = dt.NewRow
    > dr(0) = "Ben"
    > dr(1) = "Physics"
    > dr(2) = "Tuesday"
    > dt.Rows.Add(dr)
    > Dim dv As DataView = New DataView(dt)
    > Return dv
    > End Function
    >
    >"SK" <> wrote in

    message
    >news:297e01c51505$d71cb610$...
    >> Hello,
    >>
    >> does anybody know how I can add an additional row

    above
    >> the datagrid header?

    >
    >.
    >
    SK, Feb 22, 2005
    #4
  5. SK

    SK Guest

    I mean it was showing me the error:

    Specified argument was out of the range of valid values.
    Parameter name: index

    Controls(0).Controls.AddAt(2, dgItem)

    Sorry, pasted the wrong statement in the previous messahe.

    >-----Original Message-----
    >Here's some code to show two headers:
    >
    > <asp:DataGrid id="DataGrid1" runat="server"

    AutoGenerateColumns="False"
    >ShowHeader="False">
    > <Columns>
    > <asp:BoundColumn

    DataField="Subject"></asp:BoundColumn>
    > <asp:BoundColumn DataField="Day"></asp:BoundColumn>
    > </Columns>
    > </asp:DataGrid>
    >
    >
    > Dim dt As DataTable
    > Private Sub Page_Load _
    > (ByVal sender As System.Object, _
    > ByVal e As System.EventArgs) _
    > Handles MyBase.Load
    > DataGrid1.ShowHeader = True
    > DataGrid1.DataSource = CreateDataSource()
    > DataGrid1.DataBind()
    > End Sub
    > Private Sub DataGrid1_ItemDataBound _
    > (ByVal sender As Object, _
    > ByVal e As _
    > System.Web.UI.WebControls.DataGridItemEventArgs) _
    > Handles DataGrid1.ItemDataBound
    > ' Add a second header with a flag button in it
    > ' by Ken Cox Microsoft MVP [ASP.NET]
    > If e.Item.ItemType = ListItemType.Header Then
    > ' Get the collection of cells from the grid
    > Dim tcells As TableCellCollection
    > ' Create an imagebutton
    > Dim imgBtn As New ImageButton
    > ' Assign the URL
    > imgBtn.ImageUrl

    = "http://www.gc.ca/images/flag.gif"
    > ' Get the collection of existing cells so we

    can get a count
    > tcells = e.Item.Cells
    > ' Create a new cell
    > Dim fcell As New TableCell
    > ' Add the image button to the new table cell
    > fcell.Controls.Add(imgBtn)
    > ' Span the cell to however many columns there

    are
    > fcell.ColumnSpan = tcells.Count
    > ' Create a new header object
    > Dim dgItemHeader As New DataGridItem _
    > (0, 0, ListItemType.Header)
    > ' Add the cell to the header
    > dgItemHeader.Cells.Add(fcell)
    > dgItemHeader.Visible = True
    > ' Add the header to the datagrid
    > DataGrid1.Controls(0).Controls.Add

    (dgItemHeader)
    > End If
    > End Sub
    > Function CreateDataSource() As ICollection
    > ' Create sample data for the DataList control.
    > dt = New DataTable
    > Dim dr As DataRow
    >
    > ' Define the columns of the table.
    > dt.Columns.Add(New DataColumn("Student", GetType

    (String)))
    > dt.Columns.Add(New DataColumn("Subject", GetType

    (String)))
    > dt.Columns.Add(New DataColumn("Day", GetType

    (String)))
    >
    > ' Populate the table with sample values.
    > dr = dt.NewRow
    > dr(0) = "Ben"
    > dr(1) = "English"
    > dr(2) = "Thursday"
    > dt.Rows.Add(dr)
    > dr = dt.NewRow
    > dr(0) = "Ben"
    > dr(1) = "Geology"
    > dr(2) = "Monday"
    > dt.Rows.Add(dr)
    > dr = dt.NewRow
    > dr(0) = "Ben"
    > dr(1) = "Physics"
    > dr(2) = "Tuesday"
    > dt.Rows.Add(dr)
    > Dim dv As DataView = New DataView(dt)
    > Return dv
    > End Function
    >
    >"SK" <> wrote in

    message
    >news:297e01c51505$d71cb610$...
    >> Hello,
    >>
    >> does anybody know how I can add an additional row

    above
    >> the datagrid header?

    >
    >.
    >
    SK, Feb 22, 2005
    #5
    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. Vishal

    Add additional row

    Vishal, Mar 7, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    646
    Ken Cox [Microsoft MVP]
    Mar 8, 2005
  2. mlt
    Replies:
    2
    Views:
    831
    Jean-Marc Bourguet
    Jan 31, 2009
  3. stephen
    Replies:
    2
    Views:
    1,503
    Mark Stevens
    Jan 6, 2010
  4. D
    Replies:
    0
    Views:
    203
  5. datagrid having row header and column header

    , Jul 13, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    183
Loading...

Share This Page