我正在尝试向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
答案 0 :(得分:4)
由于这是一个自定义GridView,为什么不考虑重写CreateChildControls方法?
I.e(抱歉,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 :(得分:3)
好工作的人,我用你的技术分组我支持AJAX的gridview,我搜索了很长时间。欢呼声。
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();
}
答案 2 :(得分:0)
将行添加到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