Click to See Complete Forum and Search --> : Highlighting cells in a datagrid


fogofogo
09-05-2007, 07:13 AM
Hello,

I'm trying to check through a gridview and highlight cells that contain errors. The code below should check if a cell is empty and if it is, fill the background with red. I plan to add other checks, but need to get this working first.


<asp:GridView ID="errorgrid" runat="server" AutoGenerateColumns="False" EnableViewState="False" OnRowDataBound="errorgrid_RowDataBound">
<Columns>
<asp:BoundField DataField="CasinoName" HeaderText="CasinoName"/>
<asp:BoundField DataField="JTTransDate" HeaderText="JTTransDate"/>
<asp:BoundField DataField="CashinID" HeaderText="CashinID" />
</Columns>
</asp:GridView>


protected void errorgrid_RowDataBound(object sender,GridViewRowEventArgs e)
{

if (e.Row.Cells[0].Text == "")
{
//e.Row.BackColor = System.Drawing.Color.Red;
//Response.Write("Error");
errorgrid.BackColor = System.Drawing.Color.Red;
}

}



The code doesnt actually display any error message, nor does it highlight the empty cells. Anyone know what I'm doing wrong?

Thanks

guhananth
09-05-2007, 07:36 AM
protected void errorgrid_RowDataBound(object sender,GridViewRowEventArgs e)
{
if (itemType == ListItemType.Item || itemType == ListItemType.AlternatingItem)
{
if (e.Row.Cells[0].Text == "")
{
e.Row.BackColor = System.Drawing.Color.Red;
//Response.Write("Error");
// errorgrid.BackColor = System.Drawing.Color.Red;
}
}

}

Use this this will work

lmf232s
09-05-2007, 10:22 AM
I would also add this into your code. I always check to see if im dealing with the header, data, or footer row.

This is the same as what your doing with ListItemType.item or listitemtype.alternatingitem.


'You dont need the header and footer they are just there for show.
Select Case e.Row.RowType
Case DataControlRowType.Header

Case DataControlRowType.DataRow
'Your code goes here

Case DataControlRowType.Footer

End Select

fogofogo
09-05-2007, 11:51 AM
ok - thanks guys

so where you have

itemType == ListItemType.Item || itemType == ListItemType.AlternatingItem

I'd need to replace itemType with something?

lmf232s
09-05-2007, 01:30 PM
That's correct.

Now you don't have to do that its just another way to determine whether or not its a datarow and not a header or a footer row.