Click here to Skip to main content
13,191,642 members (65,773 online)
Rate this:
 
Please Sign up or sign in to vote.
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 5 days ago
Updated 5 days ago

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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;
? 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;
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
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
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.171017.2 | Last Updated 12 Oct 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100