gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:14:52 26 4
gpt4 key购买 nike

我正在尝试向 Gridview 添加新的标题行。该行应显示在原始标题行下方。

据我所知,我有两个事件可供选择:

1.) Gridview_RowDataBound2.) Gridview_RowCreated

选项 1 不是一个选项,因为网格未绑定(bind)每次回发的数据。选项 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, -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();
}

关于asp.net - 在标题后添加 Gridview 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/685717/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com