我怎么能组标题行添加到绑定的GridView数据 [英] How can I add group heading rows to a data bound GridView

查看:136
本文介绍了我怎么能组标题行添加到绑定的GridView数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个数据绑定列的GridView,就像这样:

I have a GridView with three databound columns, like this:

<Columns>
    <asp:BoundField HeaderText="Type" DataField="Type" />
    <asp:BoundField HeaderText="Amenity" DataField="Amenity" />
    <asp:BoundField HeaderText="Distance" DataField="Distance" DataFormatString="{0:0.00} Km" />
</Columns>

记录按类型排序,我想删除类型列,但随后插入一个标题行每种类型时为下一组行的值更改。我怎样才能做到这一点?

Records are sorted by type, and I want to remove the Type column, but then insert a header row for each type when the value of type for the next set of rows changes. How can I do this?

推荐答案

这是不是prettiest,但它解决了这个问题,而不打算在GridView范式之外。由于 carlj 获得的 添加(或插入)副标题行到一个GridView

This is not the prettiest, but it solves the problem without going outside of the GridView paradigm. Thanks to carlj for Adding (or Inserting) Subheader Rows into a Gridview

Dim _currentAmenityType = String.Empty
Protected Sub amenitiesGrid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles amenitiesGrid.RowDataBound
    If (e.Row.RowType = DataControlRowType.DataRow) Then
        Dim drv = e.Row.DataItem
        If (drv("Type") <> _currentAmenityType) Then
            _currentAmenityType = drv("Type")
            Dim parentTable = TryCast(e.Row.Parent, Table)
            If Not parentTable Is Nothing Then
                Dim row = New GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal)
                Dim cell = New TableCell()
                cell.ColumnSpan = amenitiesGrid.Columns.Count
                cell.Width = Unit.Percentage(100)
                cell.Style.Add("font-weight", "bold")
                cell.Style.Add("background-color", "#c0c0c0")
                cell.Style.Add("color", "white")
                Dim span = New HtmlGenericControl("span")
                span.InnerHtml = _currentAmenityType
                cell.Controls.Add(span)
                row.Cells.Add(cell)
                parentTable.Rows.AddAt(parentTable.Rows.Count - 1, row)
            End If
        End If
    End If
End Sub

这篇关于我怎么能组标题行添加到绑定的GridView数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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