5

我正在尝试向 Gridview 添加新的标题行。此行应出现在原始标题行下方。

据我所知,我有两个活动可供选择:

1.) Gridview_RowDataBound 2.) Gridview_RowCreated

选项 1 不是一个选项,因为网格没有绑定每个回发的数据。选项 2 无法按预期工作。我可以添加该行,但它是在 HeaderRow 之前添加的,因为在此事件中尚未添加 HeaderRow 本身......

请帮忙,谢谢!

代码:(InnerTable 属性由自定义 gridview 公开)

    Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.Header Then
        Dim r As New GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal)

        For Each c As DataControlField In CType(sender, GridView).Columns
            Dim nc As New TableCell
            nc.Text = c.AccessibleHeaderText
            nc.BackColor = Drawing.Color.Cornsilk
            r.Cells.Add(nc)
        Next

        Dim t As Table = GridView1.InnerTable
        t.Controls.Add(r)
    End If
End Sub
4

3 回答 3

4

既然这是自定义的 GridView,为什么不考虑重写 CreateChildControls 方法呢?

即(对不起,C#):

protected override void CreateChildControls()
{
    base.CreateChildControls();

    if (HeaderRow != null)
    {
        GridViewRow header = CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
        for (int i = 0; i < Columns.Count; i++)
        {
            TableCell cell = new TableCell();
            cell.Text = Columns[i].AccessibleHeaderText;
            cell.ForeColor = System.Drawing.Color.Black;
            cell.BackColor = System.Drawing.Color.Cornsilk;
            header.Cells.Add(cell);
        }

        Table table = (Table)Controls[0];
        table.Rows.AddAt(1, header);
    }
}

更新 正如 Ropstah 所提到的,上面的代码片段不适用于分页。我将代码移到 PrepareControlHierarchy 中,现在它可以优雅地进行分页、选择和排序。

protected override void PrepareControlHierarchy()
{
    if (ShowHeader && HeaderRow != null)
    {
        GridViewRow header = CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
        for (int i = 0; i < Columns.Count; i++)
        {
            TableCell cell = new TableCell();
            cell.Text = Columns[i].AccessibleHeaderText;
            cell.ForeColor = System.Drawing.Color.Black;
            cell.BackColor = System.Drawing.Color.Cornsilk;
            header.Cells.Add(cell);
        }

        Table table = (Table)Controls[0];
        table.Rows.AddAt(1, header);
    }

    //it seems that this call works at the beginning just as well
    //but I prefer it here, since base does some style manipulation on existing columns
    base.PrepareControlHierarchy();
}
于 2009-03-26T16:06:49.327 回答
3

干得好伙计们,我使用你的技术对启用 AJAX 的网格视图进行分组,我搜索了很长时间。干杯。

protected override void PrepareControlHierarchy()
{
    if (GroupColumns)
    {
        #region Group Column

        Table table = (Table)Controls[0];

        string lastValue = string.Empty;
        foreach (GridViewRow gvr in this.Rows)
        {
            string currentValue = gvr.Cells[GroupColumnIndex].Text;

            if (lastValue.CompareTo(currentValue) != 0)
            {
                // there's been a change in value in the sorted column
                int rowIndex = table.Rows.GetRowIndex(gvr);

                // Add a new sort header row
                GridViewRow sortRow = new GridViewRow(rowIndex, rowIndex, DataControlRowType.DataRow, DataControlRowState.Normal);

                TableCell sortCell = new TableCell();
                TableCell blankCell = new TableCell();

                sortCell.ColumnSpan = this.Columns.Count - 1;
                sortCell.Text = string.Format("{0}", currentValue);

                blankCell.CssClass = "group_header_row";
                sortCell.CssClass = "group_header_row";

                // Add sortCell to sortRow, and sortRow to gridTable
                sortRow.Cells.Add(blankCell);
                sortRow.Cells.Add(sortCell);
                table.Controls.AddAt(rowIndex, sortRow);

                // Update lastValue
                lastValue = currentValue;
            }
        }

        #endregion
    }

    HideColumns();

    base.PrepareControlHierarchy();
} 
于 2010-03-10T22:46:19.963 回答
0

将行添加到 InnerTable 时试试这个:

t.Controls.AddAt(1, r)

这是我做的一个快速的基本测试,它似乎工作正常:

Protected Sub gridview_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridview.DataBound
    Dim g As GridView = CType(sender, GridView)

    Dim r As New GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal)
    Dim th As New TableHeaderCell()
    th.ColumnSpan = g.Columns.Count
    th.Text = "This is my new header"
    r.Cells.Add(th)

    Dim t As Table = CType(g.Controls(0), Table)
    t.Rows.AddAt(1, r)
End Sub
于 2009-03-26T13:45:44.933 回答