我在 asp.net 3.5 网页中有一个 GridView 控件,以下代码在 RowDataBound 事件中执行,它会更改背景颜色和字体颜色,一旦列中的值:RegWaitTime 和 TotalWegTime 大于 30,
该值来自 sql server 中的两个计算列,它返回从其他两列减去的结果,这里的问题是,如果这些列中的值为 NULL,我将在更改颜色的代码上得到错误,,对不起我的英语,如果您需要我澄清我的要求,请告诉我,
提前致谢
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// Change Wait Time Cell Rows CHECK THE LOGIC HERE
if (e.Row.RowType == DataControlRowType.DataRow)
{
// This line will get the reference to the underlying row
DataRowView _row = (DataRowView)e.Row.DataItem;
if (_row != null)
{
// get the field value which you want to compare and
// convert to the corresponding data type
// i assume the fieldName is of int type
int _field = Convert.ToInt32(_row.Row["RegWaitTime"]);
if (_field > 30)
{
e.Row.Cells[9].BackColor = System.Drawing.Color.Red;
e.Row.Cells[9].Style.Add("color", "white");
}
else
e.Row.Cells[9].BackColor = System.Drawing.Color.Green;
e.Row.Cells[9].Style.Add("color", "white");
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
// This line will get the reference to the underlying row
DataRowView _row2 = (DataRowView)e.Row.DataItem;
if (_row2 != null)
{
// get the field value which you want to compare and
// convert to the corresponding data type
// i assume the fieldName is of int type
int _field = Convert.ToInt32(_row2.Row["TotalRegTime"]);
if (_field > 30)
{
e.Row.Cells[10].BackColor = System.Drawing.Color.Red;
e.Row.Cells[10].Style.Add("color", "white");
}
else
e.Row.Cells[10].BackColor = System.Drawing.Color.Green;
e.Row.Cells[10].Style.Add("color", "white");
}
}
}