2009-06-15 18 views
6

¿Hay alguna manera de hacer que siempre esté visible una FooterTemplate (en una GridView), incluso cuando DataSource está vacía?Mostrar siempre FooterTemplate, incluso sin datos

+0

¿Por qué quiere lograr esto? –

+0

Por favor, eche un vistazo a lo que comenté a continuación, le expliqué para qué lo necesito. – Shimmy

Respuesta

5

Si desea que se muestre siempre, independientemente de su contenido, ¿no puede simplemente poner el pie de página html fuera del GridView, en lugar de en el FooterTemplate?

Si eso no es una opción por alguna razón, entonces puede add an null row to your data source if it's empty o subclass the GridView & override the default behaviour.

Esas son las únicas opciones que conozco (aunque ha pasado un tiempo desde la última vez que usé un GridView).

+0

Realmente no me importa hacerlo con Html, el problema es que quiero que las columnas se ajusten al ancho de las columnas de GridView. Quiero que muestre un resumen cuando existen datos, y un elemento insertado (que implementé en el pie de página, ¿entiendo a qué me refiero?) Cuando se presiona el botón "Nuevo" en la plantilla del elemento, o muestra siempre el pie de página. en otras palabras: * ¿hay alguna manera de evocar mostrando el pie de página (cuando no hay datos)? * qué es realmente esta cosa de fila de datos nulos, no getcha (estoy usando EntityDataSource, creo que será más complicado o imposible en absoluto). Gracias amigo. – Shimmy

+0

No quiero usar una fila vacía, no me importa la subclasificación de GridView, pero no los datos ficticios. ¿Tienes algo? – Shimmy

+1

El segundo enlace que incluí (http://mattberseth.com/blog/2007/07/how%5Fto%5Fshow%5Fheader%5Fand%5Ffooter.html) tiene un código de ejemplo que le permite tener una cuadrícula con una propiedad ShowFooterWhenEmpty . – Alconja

7

Estaba teniendo problemas con esto también. El enlace de Alconja ayuda mucho (gracias a Alconja) pero GridView.FooterRow devuelve nulo. Lo necesito para insertar nuevos registros desde el pie de página.

Esta es mi solución final que funciona. Ahora puede insertar datos desde el pie de página incluso si la red está vacía.

GridViewExtended.cs (una clase en la carpeta App_Code):

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Text; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace YourNamespace 
{ 

    public class GridViewExtended : GridView 
    { 
    #region Public Properties 
    [Category("Behavior")] 
    [Themeable(true)] 
    [Bindable(BindableSupport.No)] 
    public bool ShowFooterWhenEmpty 
    { 
     get 
     { 
     if (this.ViewState["ShowFooterWhenEmpty"] == null) 
     { 
      this.ViewState["ShowFooterWhenEmpty"] = false; 
     } 

     return (bool)this.ViewState["ShowFooterWhenEmpty"]; 
     } 
     set 
     { 
     this.ViewState["ShowFooterWhenEmpty"] = value; 
     } 
    } 
    #endregion 

    private GridViewRow _footerRow2; 
    public override GridViewRow FooterRow 
    { 
     get 
     { 
     GridViewRow f = base.FooterRow; 
     if (f != null) 
      return f; 
     else 
      return _footerRow2; 
     } 
    } 

    protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding) 
    { 
     int rows = base.CreateChildControls(dataSource, dataBinding); 

     // no data rows created, create empty table if enabled 
     if (rows == 0 && (this.ShowFooterWhenEmpty)) 
     { 
     // create the table 
     Table table = this.CreateChildTable(); 

     DataControlField[] fields; 
     if (this.AutoGenerateColumns) 
     { 
      PagedDataSource source = new PagedDataSource(); 
      source.DataSource = dataSource; 

      System.Collections.ICollection autoGeneratedColumns = this.CreateColumns(source, true); 
      fields = new DataControlField[autoGeneratedColumns.Count]; 
      autoGeneratedColumns.CopyTo(fields, 0); 
     } 
     else 
     { 
      fields = new DataControlField[this.Columns.Count]; 
      this.Columns.CopyTo(fields, 0); 
     } 

     if (this.ShowHeaderWhenEmpty) 
     { 
      // create a new header row 
      GridViewRow headerRow = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal); 
      this.InitializeRow(headerRow, fields); 

      // add the header row to the table 
      table.Rows.Add(headerRow); 
     } 

     // create the empty row 
     GridViewRow emptyRow = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal); 
     TableCell cell = new TableCell(); 
     cell.ColumnSpan = fields.Length; 
     cell.Width = Unit.Percentage(100); 

     // respect the precedence order if both EmptyDataTemplate 
     // and EmptyDataText are both supplied ... 
     if (this.EmptyDataTemplate != null) 
     { 
      this.EmptyDataTemplate.InstantiateIn(cell); 
     } 
     else if (!string.IsNullOrEmpty(this.EmptyDataText)) 
     { 
      cell.Controls.Add(new LiteralControl(EmptyDataText)); 
     } 

     emptyRow.Cells.Add(cell); 
     table.Rows.Add(emptyRow); 

     if (this.ShowFooterWhenEmpty) 
     { 
      // create footer row 
      _footerRow2 = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal); 
      this.InitializeRow(_footerRow2, fields); 

      // add the footer to the table 
      table.Rows.Add(_footerRow2); 
     } 

     this.Controls.Clear(); 
     this.Controls.Add(table); 
     } 

     return rows; 
    } 
    } 

} 

En la página aspx , sólo tiene que añadir

<%@ Register TagPrefix="YourPrefix" Namespace="YourNamespace" %> 

y vuelva a colocar <asp:GridView con <YourPrefix:GridViewExtended

Espero que ayude a alguien.

+0

Intenté esto porque tenía una vista de cuadrícula con un pie de página con la opción de agregar un nuevo registro. Los usuarios tendrían que hacer clic en el botón "Agregar nuevo" en el pie de página después del cual verían controles para agregar una nueva fila. Este código no funciona como se esperaba (probablemente tendría que reemplazar la llamada a base.CreateChildControls con el código y establecer el id, etc.). –

+1

El problema para mí con esta solución es que el evento RowDataBound no se dispara para el pie de página por algún motivo cuando la cuadrícula está vacía. Como necesito poblar algunos desplegables allí, este es un error fatal. –

+0

Esto es genial! ¡Gracias! – jazzBox

2

Como uno de los comentaristas anteriores mencionados, el evento RowDataBound no se dispara para el pie de página. Encontré otro fragmento de código que addresses this issue, pero además de mostrar el pie de página, crea explícitamente la fila (activando el evento RowCreated) y lo vincula (activando el evento RowDataBound).

He convertido el código de referencia anterior en C# utilizando un convertidor de código y realicé algunas modificaciones menores. También incluí los comentarios que hice mientras revisaba el código para desglosarlo. Los eventos RowCreated y RowDataBound se están disparando ahora y puedo completar listas desplegables en pies de página.

using System.Linq; 
    using System.Web.UI.WebControls; 
    using System.ComponentModel; 

    namespace WebUI.Controls 
    { 
     //modified from https://stackoverflow.com/questions/3437581/show-gridview-footer-on-empty-grid 
     public class GridViewExtended : GridView 
     { 

      private GridViewRow _footerRow; 
      [DefaultValue(false), Category("Appearance"), Description("Include the footer when the table is empty")] 
      public bool ShowFooterWhenEmpty { get; set; } 

      [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)] 
      public override GridViewRow FooterRow { 
       get { 
        if ((this._footerRow == null)) { 
         this.EnsureChildControls(); 
        } 
        return this._footerRow; 
       } 
      } 

      protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding) 
      { 
       //creates all the rows that would normally be created when instantiating the grid 
       int returnVal = base.CreateChildControls(dataSource, dataBinding); 
       //if no rows were created (i.e. returnVal == 0), and we need to show the footer row, then we need to create and bind the footer row. 
       if (returnVal == 0 && this.ShowFooterWhenEmpty) { 
        Table table = this.Controls.OfType<Table>().First<Table>(); 
        DataControlField[] dcf = new DataControlField[this.Columns.Count]; 
        this.Columns.CopyTo(dcf, 0); 
        //creates the footer row 
        this._footerRow = this.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal, dataBinding, null, dcf, table.Rows, null); 
        if (!this.ShowFooter) { 
         _footerRow.Visible = false; 
        } 
       } 
       return returnVal; 
      } 

      private GridViewRow CreateRow(int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, bool dataBind, object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) 
      { 
       GridViewRow row = this.CreateRow(rowIndex, dataSourceIndex, rowType, rowState); 
       GridViewRowEventArgs e = new GridViewRowEventArgs(row); 
       if ((rowType != DataControlRowType.Pager)) { 
        this.InitializeRow(row, fields); 
       } else { 
        this.InitializePager(row, fields.Length, pagedDataSource); 
       } 
       //if the row has data, sets the data item 
       if (dataBind) { 
        row.DataItem = dataItem; 
       } 
       //Raises the RowCreated event 
       this.OnRowCreated(e); 
       //adds the row to the gridview's row collection 
       rows.Add(row); 
       //explicitly binds the data item to the row, including the footer row and raises the RowDataBound event. 
       if (dataBind) { 
        row.DataBind(); 
        this.OnRowDataBound(e); 
        row.DataItem = null; 
       } 
       return row; 
      } 

     } 

    } 
Cuestiones relacionadas