添加GridView的行头后 [英] Add Gridview Row AFTER Header

查看:118
本文介绍了添加GridView的行头后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个新的headerrow添加到GridView。该行应该出现在原来的headerrow下方。

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

1)。Gridview_RowDataBound
2)Gridview_RowCreated

选项1不象网格未结合在每次回发的数据的选项。
选项​​2不能按预期工作。我可以添加行,但是因为HeaderRow本身并不在此事件中被添加到它是在HeaderRow前加入...

请帮助,谢谢!

code:(InnerTable属性被定制gridview的暴露)

 私人小组GridView1_RowDataBound(BYVAL发件人为对象,BYVAL E上System.Web.UI.WebControls.GridViewRowEventArgs)处理GridView1.RowDataBound
    如果e.Row.RowType = DataControlRowType.Header然后
        昏暗 - [R作为新GridViewRow(-1,-1,DataControlRowType.Header,DataControlRowState.Normal)        对于每个C作为的DataControlField在CTYPE(发件人,GridView控件).Columns
            昏暗的NC作为新的TableCell
            nc.Text = c.AccessibleHeaderText
            nc.BackColor = Drawing.Color.Cornsilk
            r.Cells.Add(NC)
        下一个        昏暗T作为表= GridView1.InnerTable
        t.Controls.Add(r)的
    万一
结束小组


解决方案

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

即(对不起,C#):

 保护覆盖无效的CreateChildControls()
{
base.CreateChildControls();如果(HeaderRow!= NULL)
{
GridViewRow头= CreateRow(-1,-1,DataControlRowType.Header,DataControlRowState.Normal);
的for(int i = 0; I< Columns.Count;我++)
{
TableCell的细胞=新的TableCell();
cell.Text =列[I] .AccessibleHeaderText;
cell.ForeColor = System.Drawing.Color.Black;
cell.BackColor = System.Drawing.Color.Cornsilk;
header.Cells.Add(细胞);
}表的表=(表)控制[0];
table.Rows.AddAt(1,报头);
}
}

更新
如由Ropstah提到的,sniplet上面不与上分页工作。我搬到了code到A prepareControlHierarchy,现在它与分页,选择和排序工作正常。

 保护覆盖无效prepareControlHierarchy()
{
如果(ShowHeader&安培;&安培;!HeaderRow = NULL)
{
GridViewRow头= CreateRow(-1,-1,DataControlRowType.Header,DataControlRowState.Normal);
的for(int i = 0; I< Columns.Count;我++)
{
TableCell的细胞=新的TableCell();
cell.Text =列[I] .AccessibleHeaderText;
cell.ForeColor = System.Drawing.Color.Black;
cell.BackColor = System.Drawing.Color.Cornsilk;
header.Cells.Add(细胞);
}表的表=(表)控制[0];
table.Rows.AddAt(1,报头);
}//似乎这个呼叫之初的作品一样好
//但是我preFER在这里,因为基地确实在现有的一些列操作风格
。基地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();
}

这篇关于添加GridView的行头后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆