Comparing the values of two columns in a datagrid and manipulating the data in the columns as a result

Posted on 2006-11-16
Last Modified: 2010-04-16

I'm populating a datagrid from a database and two of the columns that are populated are a debit and credit values. For each row in the datagrid I want to compare the debit and credit columns' values. If the values are not the same, I want the debit and credit values to be displayed in red in the datagrid.

Any help would be appreciated.

Question by:GlobexCorp
  • 2
  • 2
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 17955425
what sort of grid are u using normal windows grid ?

Author Comment

ID: 17955498
It's a webcontrol grid
LVL 13

Accepted Solution

newyuppie earned 500 total points
ID: 17955597
i found this example in codeproject:

this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.OnNWDataBound);

private void OnNWDataBound(object sender,
             System.Web.UI.WebControls.DataGridItemEventArgs e)
    if (e.Item.ItemType == ListItemType.Item ||
        e.Item.ItemType == ListItemType.AlternatingItem)
        DataRowView rv = (DataRowView)e.Item.DataItem;
        // Get fourth column value.
        Int32 nUnitsInStock = Convert.ToInt32(rv.Row.ItemArray[4]);
        if (nUnitsInStock < 20)
           e.Item.Cells[4].BackColor = Color.Red;

it shows how to get values from a column, how to change background color, etc. it just needs a little modification. hoep it helps a bit


Author Comment

ID: 17955841
Thanks newyuppie!
LVL 13

Expert Comment

ID: 17955863
sure, thanks for the points!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Image(7) 1 53
Connecting database in Xamarin? 6 53
c# string handling 1 27
C# Comparing String Arrays. 5 26
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Sending a Secure fax is easy with eFax Corporate ( First, just open a new email message. In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now