2017-10-12 4 views
1

Je suis nouveau dans ASPX et j'ai une grille comme ceci:Utilisation validation GridView avec BoundField

<asp:GridView ID="grdViewTareas" AutoGenerateColumns="false" runat="server" CssClass="Grid"> 
    <Columns> 
    <asp:BoundField DataField="stardate" HeaderText="Fecha Inicio" DataFormatString="{0:d}" ItemStyle-HorizontalAlign="Center" /> 
    <asp:BoundField DataField="duedate" HeaderText="Fecha Fin" DataFormatString="{0:d}" ItemStyle-HorizontalAlign="Center" /> 
    <asp:BoundField DataField="progress" HeaderText="% de Avance" ItemStyle-HorizontalAlign="Center" /> 
    </Columns> 
</asp:GridView> 

Je veux faire une validation pour chaque ligne de la grille pour la peinture fond de la colonne par exemple:

if (progress < 100){ 
background-color: red; 
} 

Comment puis-je y parvenir. Cordialement

Répondre

1

Vous utilisez l'événement OnRowDataBound pour cela.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    //check if the row is a datarow 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     //cast the row back to a datarowview 
     DataRowView row = e.Row.DataItem as DataRowView; 

     DateTime _currentDate = new DateTime(); 
     DateTime _dueDate = new DateTime(); 

     //first, check if the date fields are null or empty and then try to convert 
     if (!string.IsNullOrEmpty(row["currentDate"].ToString())) 
     { 
      _currentDate = Convert.ToDateTime(row["currentDate"]); 
     } 

     if (!string.IsNullOrEmpty(row["dueDate"].ToString())) 
     { 
      _dueDate = Convert.ToDateTime(row["dueDate"]); 
     } 

     //check the value of progress and set the background color 
     if (Convert.ToInt32(row["progress"]) < 100 && _currentDate > _dueDate) 
     { 
      e.Row.Cells[0].BackColor = Color.Red; 
     } 
    } 
} 

Vous devez ajouter OnRowDataBound="GridView1_RowDataBound" au GridView.

Si vous avez lié une liste des classes List<Myclass> vous faites ceci:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    //check if the row is a datarow 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     //if you bound a list of classes to the GridView, cast back to the orignal class 
     MyClass item = e.Row.DataItem as MyClass; 

     //check the propertyu value of the class and set the background color 
     if (item.progress < 100 && item.currentDate > item.dueDate) 
     { 
      e.Row.Cells[0].BackColor = Color.Red; 
     } 
    } 
} 
+0

et si je veux ajouter 'et' conditionnelle je viens d'utiliser comme si' (Convert.ToInt32 (ligne [ « progrès »]) <100 && row ["stardate"]> row ["duedate"]) 'ou je me trompe? – Pepe

+0

Oui, mais vous devez convertir en DateTime 'Convert.ToDateTime (row [" duedate "])' – VDWWD

+0

Pouvez-vous vérifier ma modification de votre réponse s'il vous plaît? Si j'utilise juste la validation de "progression" cela fonctionne: 'if (progression <100)' mais si j'ajoute la validation de Date comme '(progression <100 && currentDate> dueDate)' elle renvoie erreur – Pepe