【问题标题】:Add Gridview Row AFTER Header在标题之后添加 Gridview 行
【发布时间】:2010-10-15 16:52:45
【问题描述】:

我正在尝试向 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

【问题讨论】:

  • 不,这不起作用。与我最初问题中的选项 1 相同的问题...
  • 您答案中的代码是否正确?我也有同样的问题。
  • 我设法让它工作......在我的情况下,我需要知道 CreateRow 函数,它允许我构建我需要的功能。祝你好运。

标签: asp.net gridview dynamic row add


【解决方案1】:

既然这是一个自定义的 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();
}

【讨论】:

  • 我不同意,起初它似乎有效,但是当您启用分页/选择时,您会得到不需要的结果。 1.) 分页使该行消失。 2.)选择使该行再次出现,但单元格内容是第一个数据行的内容,而不是可访问的标题文本...
  • 你是对的。我没有测试分页和选择。接得好。看看它应该去哪里会很有趣。
【解决方案2】:

干得好伙计们,我使用你的技术对启用 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();
} 

【讨论】:

    【解决方案3】:

    将行添加到 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多