How to create custom header in ASP.NET Gridview?

半世苍凉 提交于 2019-12-18 09:41:21

问题


I have a GridView in which I will be performing CRUD operations.(therefore i am using templatefields) . I am trying to make my grid look something like this:

Each cell will contain textbox etc. If you notice the columns there are multiple columns within each. How can I do so?

I came across http://www.aspsnippets.com/Articles/ASPNet-GridView-Group-Header-Row-Columns-and-display-Multiple-Columns-under-Single-Column.aspx but this doesnt seem to fulfill my needs.


回答1:


Here is an example code behind from one of my GridView's for doing just that in the GridView's PreRender event. In this example I'm actually adding two additional rows above the original Header. As you can see I'm adjusting the Colspans of the new cells. Forgive the VB:

    Private Sub gvExpertRateHistory_PreRender(sender As Object, e As System.EventArgs) Handles gvExpertRateHistory.PreRender
        Dim this As GridView = sender
        Dim InnerTable As Table = If(this.HasControls(), this.Controls(0), Nothing)

        If this.HeaderRow IsNot Nothing AndAlso InnerTable IsNot Nothing Then
            Dim hr As GridViewRow

            hr = New GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal)

            hr.Cells.Add(NewCell(1, String.Empty, this, , HorizontalAlign.Left))
            hr.Cells.Add(NewCell(2, "Requested On", this, , HorizontalAlign.Left))
            hr.Cells.Add(NewCell(4, "Review Rates", this, "WhiteBorderLB"))
            hr.Cells.Add(NewCell(6, "Court Rates", this, "WhiteBorderLB"))
            hr.Cells.Add(NewCell(6, "Deposition Rates", this, "WhiteBorderLB"))
            hr.Cells.Add(NewCell(4, "IME Rates", this, "WhiteBorderLB"))
            InnerTable.Rows.AddAt(0, hr)

            hr = New GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal)

            hr.Cells.Add(NewCell(1, "Expert", this, , HorizontalAlign.Left))
            hr.Cells.Add(NewCell(2, "Requested By", this, , HorizontalAlign.Left))
            hr.Cells.Add(NewCell(2, "Hourly", this, "WhiteBorderLB"))
            hr.Cells.Add(NewCell(2, "Flat", this, "WhiteBorderLB"))
            hr.Cells.Add(NewCell(2, "Hourly", this, "WhiteBorderLB"))
            hr.Cells.Add(NewCell(2, "Daily", this, "WhiteBorderLB"))
            hr.Cells.Add(NewCell(2, "Half-Day", this, "WhiteBorderLB"))
            hr.Cells.Add(NewCell(2, "Hourly", this, "WhiteBorderLB"))
            hr.Cells.Add(NewCell(2, "Daily", this, "WhiteBorderLB"))
            hr.Cells.Add(NewCell(2, "Half-Day", this, "WhiteBorderLB"))
            hr.Cells.Add(NewCell(2, "Hourly", this, "WhiteBorderLB"))
            hr.Cells.Add(NewCell(2, "Flat", this, "WhiteBorderLB"))
            InnerTable.Rows.AddAt(1, hr)
        End If
    End Sub

This is a Helper function that makes it easier to add new cells.

Note:

  • There is also a RowSpan property in the TableHeaderCell class if needed
  • Also, AddCssClass() is a custom extension function of mine.

    Private Function NewCell(colspan As Int32, 
                             text As String, 
                             gv As GridView, 
                             Optional CssClass As String = "", 
                             Optional Alignment As HorizontalAlign = HorizontalAlign.Center
                           ) As TableHeaderCell
    
        Dim thc As New TableHeaderCell
    
        thc.HorizontalAlign = Alignment
        thc.ColumnSpan = colspan
        thc.Text = text
        thc.BackColor = gv.HeaderRow.BackColor
        thc.ForeColor = gv.HeaderRow.ForeColor
        thc.Font.Bold = True
        thc.AddCssClass(CssClass)
    
        Return thc
    
    End Function
    


来源:https://stackoverflow.com/questions/36716084/how-to-create-custom-header-in-asp-net-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!