Click here to Skip to main content
15,911,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to compare two gridview columns and only color the ones that are different.

What i tried below just colors the whole column zero without matching.

What I have tried:

protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
       {
           if (e.Row.RowType == DataControlRowType.DataRow)
           {

               if (DataBinder.Eval(e.Row.DataItem, "RockwellPartno") != DataBinder.Eval(e.Row.DataItem, "Part_Number"))
               {
                   e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;
               }
           }
       }
Posted
Updated 12-Oct-17 11:06am

1 solution

The property e.Row.DataItem is not available until the RowDataBound event has fired, from memory this is fired after the RowCreated event

If you debug this you will probably find that these values are null, try as follows;
a) Add a break-point to you RowCreated event handler
b) When you are in the event handler, open your Immediate window in Visual Studio & enter the following;
C#
? DataBinder.Eval(e.Row.DataItem, "RockwellPartno")

This will tell you what the value of your expression is - if it is null & the other expression is also null there is your issue. Nulls are not equal hence the expression is false.

From memory, to access the values in the datagrid you need to do either of the following;
a) Access the inner text value of the field in the code behind;
C#
e.Row.Cells[0].Text


b) Add the required fields to the DataKeyNames property of your data-grid in your ASPX page & then access the values in your code behind using the following
C#
GridView1.DataKeys[e.Row.RowIndex]["RockwellPartno"].ToString()


Use your debugger & read the documentation.
The Immediate & Local windows in your debugger will show you what values you have access to & will allow you to figure out what you can & cannot do.
MSDN contains literally thousands of examples & the documentation is typically very detailed. The below link is the MSDN documentation for the GridView control

GridViewRow Class (System.Web.UI.WebControls)[^]

Kind Regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900