C# 为ASP.NET GridView中的所有空单元格着色
我想知道是否有办法将GridView中的所有空单元格都涂成橙色。我的GridView中的列是动态生成的。感谢您的帮助 谢谢C# 为ASP.NET GridView中的所有空单元格着色,c#,asp.net,.net,gridview,cells,C#,Asp.net,.net,Gridview,Cells,我想知道是否有办法将GridView中的所有空单元格都涂成橙色。我的GridView中的列是动态生成的。感谢您的帮助 谢谢 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { //check if the rowtype is a datarow if (e.Row.RowType == DataControlRowType.DataRow) {
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;
}
}
试试这个:
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与字符串进行比较。那不行
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;
}
}
}
}
您可以使用RowDataBound事件来实现这一点,就像您现在所做的那样。但有一个问题。创建一个int值,但如果value==,则尝试将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;
}
}
}
}
OnRowDataBound事件是您的起点。在那里你可以循环所有的单元格并检查它们的值。看。你能告诉我你是怎么做的吗?我有没有值的单元格。如何筛选这些列?另外,我不确定将获得多少列,因为gridview是动态的。OnRowDataBound事件是您的起点。在那里你可以循环所有的单元格并检查它们的值。看。你能告诉我你是怎么做的吗?我有没有值的单元格。你是如何过滤这些内容的?另外,我也不确定会得到多少列,因为gridview是动态的。谢谢你的帮助!谢谢你的帮助!