IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

ASP.NET Discussion :

Afficher le footer d'un gridview avec gridview datasource vide


Sujet :

ASP.NET

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2011
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2011
    Messages : 1
    Par défaut Afficher le footer d'un gridview avec gridview datasource vide
    Bonjour,
    je voudrais afficher le footer d'un gridview pour ajouter un nouvel élément, mais le datosource de la gridview est vide, ça génère une erreur comme quoi il n y a pas d'instance...
    J'ai beau chercher sur le net, tout le monde a ce problème, et personne n'est arrivé à le résoudre jusque la.
    Merci pour l'aide !

  2. #2
    Futur Membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juin 2011
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2011
    Messages : 2
    Par défaut
    Oui tu as raison, ce problème est souvent rencontré sur le net par les développeurs même dans ce forum, cela confirme les problèmes de l'asp.net qui restent insolvables, malheureusement !


  3. #3
    Modérateur

    Homme Profil pro
    Chef de projet NTIC
    Inscrit en
    Avril 2007
    Messages
    1 996
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : Service public

    Informations forums :
    Inscription : Avril 2007
    Messages : 1 996
    Par défaut
    Ce n'est pas "insolvable"... Il suffit du binder à la grid un item non vide type un DataTable contenant une datarow dont une datacolumn aurait la valeur " ".

  4. #4
    Membre Expert
    Avatar de jbrasselet
    Homme Profil pro
    Chef de projet NTIC
    Inscrit en
    Mars 2006
    Messages
    1 022
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2006
    Messages : 1 022
    Par défaut
    Une autre solution (que j'ai déjà faite) est de créer une classe hérité de la gridview et de surcharger le CreateChildControls.

    Voici un exemple de ce que j'avais fait
    Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    [ToolboxData("<{0}:GridViewWithAlwaysShowFooter runat=server></{0}:GridViewWithAlwaysShowFooter>")]
       public class GridViewWithAlwaysShowFooter : GridView
       {
    private GridViewRow _headerRow;
          private GridViewRow _footerRow;
     
          private bool _showFooterWhenEmpty;
     
          [Category("Behavior")]
          [Themeable(true)]
          [Bindable(BindableSupport.No)]
          public bool ShowFooterWhenEmpty
          {
             get { return _showFooterWhenEmpty; }
             set { _showFooterWhenEmpty = value; }
          }
     
    public override GridViewRow HeaderRow
          {
             get { return base.HeaderRow ?? _headerRow; }
          }
     
          public override GridViewRow FooterRow
          {
             get { return base.FooterRow ?? _footerRow; }
          }
     
    protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
          {
            int rows = base.CreateChildControls(dataSource, dataBinding);
     
             if (rows == 0 && (ShowFooterWhenEmpty || ShowHeaderWhenEmpty))
             {
     
    // create the table
                Table table = CreateChildTable();
     
                Controls.Clear();
                Controls.Add(table);
     
                DataControlField[] fields;
                if (AutoGenerateColumns)
                {
                   PagedDataSource source = new PagedDataSource();
                   source.DataSource = dataSource;
     
                   ICollection autoGeneratedColumns = CreateColumns(source, true);
                   fields = new DataControlField[autoGeneratedColumns.Count];
                   autoGeneratedColumns.CopyTo(fields, 0);
                }
                else
                {
                   fields = new DataControlField[Columns.Count];
                   Columns.CopyTo(fields, 0);
                }
     
                TableRowCollection newRows = table.Rows;
                if (ShowHeaderWhenEmpty)
                {
                   // create a new header row
                   _headerRow = CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
                   InitializeRow(_headerRow, fields, newRows);
                }
     
                // 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 (EmptyDataTemplate != null)
                {
                   EmptyDataTemplate.InstantiateIn(cell);
                }
                else if (!string.IsNullOrEmpty(EmptyDataText))
                {
                   cell.Controls.Add(new LiteralControl(EmptyDataText));
                }
     
                emptyRow.Cells.Add(cell);
                GridViewRowEventArgs e = new GridViewRowEventArgs(emptyRow);
                OnRowCreated(e);
     
                newRows.Add(emptyRow);
     
                emptyRow.DataBind();
                OnRowDataBound(e);
                emptyRow.DataItem = null;
     
                if (ShowFooterWhenEmpty)
                {
                   // create footer row
                   _footerRow = CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
                   InitializeRow(_footerRow, fields, newRows);
                }
                }
     
    return rows;
          }
     
    private void InitializeRow(GridViewRow row, DataControlField[] fields, TableRowCollection newRows)
          {
             GridViewRowEventArgs e = new GridViewRowEventArgs(row);
             InitializeRow(row, fields);
             OnRowCreated(e);
     
             newRows.Add(row);
     
             row.DataBind();
             OnRowDataBound(e);
             row.DataItem = null;
          }
        }

    NB : Ce n'est pas très très commenté mais on s'y retrouve
    NB2 : Désolé pour le ET dans le code mais il y a un plantage du forum quand je tente de poster avec le &&

Discussions similaires

  1. Utilisation d'un dataset avec un datasource vide
    Par lylynath dans le forum Jasper
    Réponses: 1
    Dernier message: 07/11/2013, 13h17
  2. Afficher images en thumbs avec GridView
    Par tamtoum1987 dans le forum Android
    Réponses: 1
    Dernier message: 14/03/2013, 14h27
  3. [Débutant] afficher Gridview avec champs vide permettant l'ajout de donnée
    Par Linconnu dans le forum ASP.NET
    Réponses: 2
    Dernier message: 30/07/2012, 09h39
  4. Réponses: 3
    Dernier message: 26/07/2006, 10h18
  5. Réponses: 2
    Dernier message: 20/06/2006, 16h32

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo