如何检测网格视图空单元格?我需要它来突出显示。所以我做了一个css
.RedColored
{
background: FF0000;
}
并尝试以这种方式将其显示为清空GV细胞:
protected virtual GridView1_RowDataBound (_sender : object, e : System.Web.UI.WebControls.GridViewRowEventArgs) : void
{
e.Row.Cells[0].CssClass = "wide";
foreach(i : int in [0..e.Row.Cells.Count-1])
{
when(e.Row.Cells[i].Text==null)
{
e.Row.Cells[i].CssClass="RedColored";
}
}
}
但即使我尝试过,我的它似乎也没有空单元格 Text ==“”,Cell [i] == null,Cell [i] .ToString()==“”并没有任何帮助。
recoded to :
def IsCellNull(cell : TableCell) : bool
{
| null => true
| c => string.IsNullOrEmpty(c.ToString()) || c.GetType().Name == "DBNull"
}
foreach(i : int in [0..e.Row.Cells.Count-1])
{
when(IsCellNull(e.Row.Cells[i]))
{
e.Row.Cells[i].Text="BLABLA";
e.Row.Cells[i].CssClass="RedColored";
}
}
但是!!!它甚至没有帮助,它没有WHEN,但是当(if)找不到空单元格时:P 最后:用这段代码解决:`e.Row.Cells [0] .CssClass =“wide”;
def IsCellNull(cell : TableCell) : bool
{
| null => true
| c => string.IsNullOrEmpty(c.ToString())
|| c.GetType().Name == "DBNull"
|| c.Text==" "
}
foreach(i : int in [0..e.Row.Cells.Count-1])
{
when(IsCellNull(e.Row.Cells[i]))
{
e.Row.Cells[i].BackColor=System.Drawing.Color.Red;
}
}`
答案 0 :(得分:3)
"When a bound GridView cell contains no data, ASP.NET fills it with Hence the length of 6"
http://bytes.com/topic/asp-net/answers/768857-how-check-if-cell-gridview-empty
答案 1 :(得分:1)
在某些浏览器中,如果单元格为空,则单元格不会显示颜色。在其中添加空格可以解决此问题,但随后它将不再为空...
要测试单元格,您应该使用string.IsNullOrEmpty():
when(string.IsNullOrEmpty(e.Row.Cells[i].Text))
{
e.Row.Cells[i].Text=" "; // Or sometimes a no break space - will work better
e.Row.Cells[i].CssClass="RedColored";
}
答案 2 :(得分:1)
我以前为此编写了一个实用程序函数,它是在VB.NET中但转换为C#应该非常简单
公共共享功能 IsCellBlank(ByVal cell As DataGridViewCell)作为布尔值
If (cell.Value Is Nothing) Then Return True End If If (cell.Value.ToString().Length = 0)
然后
Return True End If If (cell.Value.GetType().Name = "DBNull") Then Return True End If Return False End Function
答案 3 :(得分:1)
试试这个:
protected void grdList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[3].Text.Equals(" "))
{
e.Row.Cells[3].BackColor = Color.Red;
}
}
}