Reputation: 33744
how to detect grid view empty cell ? I need it for highlighting. So I made a css
.RedColored
{
background: FF0000;
}
and trying to appear it to empty GV cells this way :
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";
}
}
}
but my it doesn't appears to empty cells , even I've tried Text=="" , Cell[i]==null, Cell[i].ToString()=="" and nothing helped.
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";
}
}
But !!! It even doesn't helped , it works without WHEN, but when (if) can not find empty cells :P Finally : solved with this code :` 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;
}
}`
Upvotes: 0
Views: 14818
Reputation: 9414
Try this:
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;
}
}
}
Upvotes: 1
Reputation: 181
"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
Upvotes: 3
Reputation: 498992
In some browsers, the cell will not display a color if it is empty. Adding a space in it solves this issue, but then it will no longer be empty...
To test a cell, you should use 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";
}
Upvotes: 1
Reputation: 11
I have wrote a utility function for this in the past, it is in VB.NET but should be pretty straighforward to convert to C#
Public Shared Function IsCellBlank(ByVal cell As DataGridViewCell) As Boolean
If (cell.Value Is Nothing) Then Return True End If If (cell.Value.ToString().Length = 0)
Then
Return True End If If (cell.Value.GetType().Name = "DBNull") Then Return True End If Return False End Function
Upvotes: 1