加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 在标题后添加Gridview行

发布时间:2020-12-16 07:07:32 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试向Gridview添加新的headerrow.此行应显示在原始标题下方. 据我所知,我有两个可供选择的活动: 1.)Gridview_RowDataBound 2.)Gridview_RowCreated 选项1不是一个选项,因为网格没有绑定每个回发上的数据. 选项2无法按预期工作.我可以添加行,但它会在
我正在尝试向Gridview添加新的headerrow.此行应显示在原始标题下方.

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

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

解决方法

由于这是一个自定义GridView,为什么不考虑重写CreateChildControls方法?

我(抱歉,C#):

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

    if (HeaderRow != null)
    {
        GridViewRow header = CreateRow(-1,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);
    }
}

UPDATE
正如Ropstah所提到的,上面的剪辑不适用于分页.我将代码移动到PrepareControlHierarchy,现在它可以优雅地进行分页,选择和排序.

protected override void PrepareControlHierarchy()
{
    if (ShowHeader && HeaderRow != null)
    {
        GridViewRow header = CreateRow(-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();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读