Ajouter Gridview ligne après en-tête
Question
Je suis en train d'ajouter une nouvelle headerrow à un Gridview. Cette ligne devrait apparaître sous le headerrow d'origine.
Pour autant que je sais que j'ai deux événements à choisir:
1). Gridview_RowDataBound 2.) Gridview_RowCreated
Option 1 est pas une option que la grille ne lie pas les données sur chaque publication. Option 2 ne fonctionne pas comme prévu. Je peux ajouter la ligne, mais il est ajouté avant le HeaderRow parce que le HeaderRow lui-même est pas encore ajouté à cet événement ...
S'il vous plaît aider, je vous remercie!
Code: (propriété innertable est exposé par gridview personnalisé)
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
La solution
Comme il est un GridView personnalisé, pourquoi ne vous considérez pas redéfinissant la méthode CreateChildControls?
i.e. (désolé, 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 Comme il a été mentionné par Ropstah, le sniplet ci-dessus ne fonctionne pas avec le pagination. Je me suis déplacé le code à un PrepareControlHierarchy et maintenant il fonctionne grâce à la pagination, la sélection et le tri.
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();
}
Autres conseils
les gars de travail Nice, j'ai utilisé votre technique pour grouper mon AJAX permis gridview, et j'ai cherché longtemps, très longtemps. Vive.
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();
}
Essayez ceci lorsque vous ajoutez la ligne à la innertable:
t.Controls.AddAt(1, r)
Voici un test rapide de base que je l'ai fait, ce qui semble fonctionner OK:
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