Click here to Skip to main content
13,665,931 members
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 12-Oct-17 8:39am
Updated 12-Oct-17 11:06am

1 solution

Rate this: bad
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;

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

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

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 | Cookies | Terms of Service
Web06-2016 | 2.8.180810.1 | Last Updated 12 Oct 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

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