ASP.NET GridView | How to build a footer like exact copy of header? (Visual and Methods)
I´m extending the Gridview. Right now, i have this:
My goal is to change it till i get this (Paint):
I think that it can be done thru the "OnRowCreated". But dont have a clue on how.
protected override开发者_如何学JAVA void OnRowCreated(GridViewRowEventArgs e)
{
base.OnRowCreated(e);
if (e.Row.RowType == DataControlRowType.Footer)
{
}
}
Can anyone help me out on this one?
Regards
According to your question in JonH's comments, if there is an easier way to put the header's text into the footer, set every footer's cell-text same as header (Vb.Net):
Private Sub MyGrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.Footer Then
Dim header As GridViewRow = DirectCast(sender, GridView).HeaderRow
For i As Int32 = 0 To header.Cells.Count - 1
Dim headerCell As TableCell = header.Cells(i)
Dim footerCell As TableCell = e.Row.Cells(i)
footerCell.Text = headerCell.Text
Next
End If
End Sub
You should use the same CssClass on HeaderStyle and FooterStyle to apply the same style.
If you want to add controls dynamically(like the Checkbox in your image), you should use RowCreated-Event instead(like Tomas Voracek suggested), because controls must be regenerated on every postback and RowDataBound gets only called when you bind the datasource to the grid.
Just change it to
if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.Footer)
{
// add cells to row
}
Set the gridview's ShowFooter property to true. Then you have the right idea, you'll want to first give some style to your footer as it will only appear as white when you set ShowFooter=True.
Once you apply some CSS to the footer property settings you will see some color. The in the RowCreatedEvent event do a check:
if (e.Row.RowType == DataControlRowType.DataRow)
{
//dont do anything
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "MyCol1";
e.Row.Cells[2].Text = "MyCol2";
e.Row.Cells[3].Text = "MyCol3";
}
Here's an article in case you need additional help
精彩评论