如何创建ASP.NET的GridView自定义标题? [英] How to create custom header in ASP.NET Gridview?

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

问题描述

我有一个GridView中,我将要执行的CRUD操作。(所以我使用TemplateField)。
我试图让我的网格是这个样子:

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?

我碰到<少时href=\"http://www.aspsnippets.com/Articles/ASPNet-GridView-Group-Header-Row-Columns-and-display-Multiple-Columns-under-Single-Column.aspx\" rel=\"nofollow\">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的一个在后面的例子code只是这样做,在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 属性

    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天全站免登陆