如何在 ASP.NET Gridview 中创建自定义标头? [英] How to create custom header in ASP.NET Gridview?

查看:18
本文介绍了如何在 ASP.NET Gridview 中创建自定义标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GridView,我将在其中执行 CRUD 操作.(因此我使用模板字段).我试图让我的网格看起来像这样:

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?

我遇到了 http://www.aspsnippets.com/Articles/ASPNet-GridView-Group-Header-Row-Columns-and-display-Multiple-Columns-under-Single-Column.aspx 但这似乎不能满足我的需求.

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.

推荐答案

这里是我的一个 GridView 背后的示例代码,用于在 GridView 的 PreRender 事件中执行此操作.在这个例子中,我实际上是在原始标题上方添加了两行.如您所见,我正在调整新单元格的 Colspans.原谅 VB:

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.

注意:

  • 如果需要,TableHeaderCell 类中还有一个 RowSpan 属性
  • 另外,AddCssClass() 是我的自定义扩展函数.

  • 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

这篇关于如何在 ASP.NET Gridview 中创建自定义标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆