为 ASP.NET GridView 中的所有空单元格着色
Color All Empty Cells in ASP.NET GridView
我想知道是否有办法将 GridView 中的所有空单元格着色为橙色。我的 GridView 中的列是动态生成的。感谢任何帮助。
谢谢!
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the rowtype is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//loop all the cells in the row
for (int i = 0; i < e.Row.Cells.Count; i++)
{
int value = 0;
//try converting the cell value to an int
try
{
value = Convert.ToInt32(e.Row.Cells[i].Text);
}
catch
{
}
//check the value and set the background color
if (value == "")
{
e.Row.Cells[i].BackColor = Color.Green;
}
else
{
e.Row.Cells[i].BackColor = Color.White;
}
}
}
}
试试这个:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Text == " ")
e.Row.Cells[i].BackColor = Color.Orange;
}
}
您可以像现在一样使用 RowDataBound
事件。但有一个问题。您创建了一个 int value
,但随后您尝试将 value
与字符串 if (value == "")
进行比较。那行不通的。
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;
//loop all columns in the row
for (int i = 0; i < e.Row.Cells.Count; i++)
{
//check if the string is null of empty in the source data
//(row[i]) instead of e.Row.Cells[i].Text
if (string.IsNullOrEmpty(row[i].ToString()))
{
e.Row.Cells[i].BackColor = Color.Green;
}
else
{
e.Row.Cells[i].BackColor = Color.White;
}
}
}
}
我想知道是否有办法将 GridView 中的所有空单元格着色为橙色。我的 GridView 中的列是动态生成的。感谢任何帮助。
谢谢!
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the rowtype is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//loop all the cells in the row
for (int i = 0; i < e.Row.Cells.Count; i++)
{
int value = 0;
//try converting the cell value to an int
try
{
value = Convert.ToInt32(e.Row.Cells[i].Text);
}
catch
{
}
//check the value and set the background color
if (value == "")
{
e.Row.Cells[i].BackColor = Color.Green;
}
else
{
e.Row.Cells[i].BackColor = Color.White;
}
}
}
}
试试这个:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Text == " ")
e.Row.Cells[i].BackColor = Color.Orange;
}
}
您可以像现在一样使用 RowDataBound
事件。但有一个问题。您创建了一个 int value
,但随后您尝试将 value
与字符串 if (value == "")
进行比较。那行不通的。
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;
//loop all columns in the row
for (int i = 0; i < e.Row.Cells.Count; i++)
{
//check if the string is null of empty in the source data
//(row[i]) instead of e.Row.Cells[i].Text
if (string.IsNullOrEmpty(row[i].ToString()))
{
e.Row.Cells[i].BackColor = Color.Green;
}
else
{
e.Row.Cells[i].BackColor = Color.White;
}
}
}
}