且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

添加Gridview行AFTER标题

更新时间:2023-12-06 17:19:46

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



Ie(对不起,C#):

  protected override void CreateChildControls()
{
base。的CreateChildControls();

if(HeaderRow!= null)
{
GridViewRow标头= CreateRow(-1,-1,DataControlRowType.Header,DataControlRowState.Normal); (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);
}

表表=(表)控件[0];
table.Rows.AddAt(1,header);
}
}

更新
如Ropstah所提到的,上面的sniplet不能与分页。我将代码移动到PrepareControlHierarchy,现在它使用分页,选择和排序工作正常。

  protected override void PrepareControlHierarchy() 
{
if(ShowHeader&& HeaderRow!= null)
{
GridViewRow header = CreateRow(-1,-1,DataControlRowType.Header,DataControlRowState.Normal); (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);
}

表表=(表)控件[0];
table.Rows.AddAt(1,header);
}

//似乎这个调用在起始时一样也是
//但是我喜欢这里,因为base对现有的列执行了一些样式操作
base.PrepareControlHierarchy();
}


i'm trying to add a new headerrow to a Gridview. This row should appear below the original headerrow.

As far as I know I have two events to choose from:

1.) Gridview_RowDataBound 2.) Gridview_RowCreated

Option 1 is not an option as the grid is not binding the data on each postback. Option 2 does not work as expected. I can add the row, but it is added before the HeaderRow because the HeaderRow itself is not added yet in this event...

Please assist, thank you!

Code: (InnerTable property is exposed by custom 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

Since this is a custom GridView, why don't you consider overriding the CreateChildControls method?

I.e (sorry, 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);
    }
}

UPDATE As was mentioned by Ropstah, the sniplet above does not work with pagination on. I moved the code to a PrepareControlHierarchy and now it works gracefully with pagination, selection, and sorting.

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();
}