我是aspx的新手,我有这样的网格:
<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>
我想对每一行网格进行验证,以绘制列的背景,例如:
if (progress < 100){
background-color: red;
}
我怎样才能做到这一点。此致
答案 0 :(得分:1)
您可以使用OnRowDataBound
事件。
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;
}
}
}
您需要将OnRowDataBound="GridView1_RowDataBound"
添加到GridView。
如果绑定了类List<Myclass>
的列表,则执行此操作:
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;
}
}
}