Skip to content

Infragistics Community Forum / Desktop / Ultimate UI for Windows Forms / Change Ultragrid row color based on value

Change Ultragrid row color based on value

New Discussion
Clint Fisher
Clint Fisher asked on Jun 22, 2020 1:37 PM

I’m trying to highlight all rows when duplicate values are found. When I use the below, it only changes the background of the first row it finds.

I am applying formatting to the grid using presets. I tried removing that but nothing changed.

private void ultraGrid2_InitializeRow(object sender, InitializeRowEventArgs e)
{
var dups = _dtHosts.AsEnumerable()
.GroupBy(r => r[“Hostname”]).Where(gr => gr.Count() > 1).ToList();

foreach (var dup in dups)
{
foreach (UltraGridRow row in ultraGrid2.Rows)
{
if (row.Cells[“Hostname”].Value == dup.Key)
{
row.Cells[“Hostname”].Appearance.BackColor = Color.OrangeRed;
}
}
}
}

Sign In to post a reply

Replies

  • 0
    Andrew Goldenbaum
    Andrew Goldenbaum answered on Jun 19, 2020 7:32 PM

    Hello Clint,

    I have been investigating into the behavior you are seeing, and at the moment I am unsure of why this would be only have an effect on the first row it finds as I have put together a sample project to test this and that does not happen. I do have a recommendation for you on this matter, though.

    My first recommendation on this matter is to originally do this in the UltraGrid’s InitializeLayout rather than in InitializeRow. The reasoning for this is that by doing this initially in InitializeRow, you will be potentially looping through all of your rows each time a new one is initialized, whereas this can be done once per “GroupBy” in InitializeLayout, and then you can hook InitializeRow to handle any value changes. This will potentially get around any possible performance issues with this behavior.

    I am attaching the sample project I used to test this. I hope it helps you.

    Please let me know if you have any other questions or concerns on this matter.

    • 0
      Clint Fisher
      Clint Fisher answered on Jun 19, 2020 8:28 PM

      Really odd changed this line:

      if (row.Cells[“Hostname”].Value == dup.Key)

      to:

      if ((string)row.Cells[“Hostname”].Value == (string)dup.Key)

      I dropped in some console output and was able to see that after the first match, even though it was comparing the same values it would not resolve true to color the cell. Once I added in (string), everything works great.

      • 0
        Mike Saltzman
        Mike Saltzman answered on Jun 22, 2020 1:14 PM

        The Value property on a cell is type Object, so that might be why. It might be doing a reference comparison, especially if Key is also an object. 

        You might want to consider using string.Compare or string.Equals rather than "==". 

      • 0
        Clint Fisher
        Clint Fisher answered on Jun 22, 2020 1:37 PM

        That's works as well. Thanks!

  • 0
    Ivan Kitanov
    Ivan Kitanov answered on Feb 23, 2026 9:40 AM

    Hello Clint,

    I have been investigating into the behavior you are seeing, and at the moment I am unsure of why this would be only have an effect on the first row it finds as I have put together a sample project to test this and that does not happen. I do have a recommendation for you on this matter, though.

    My first recommendation on this matter is to originally do this in the UltraGrid’s InitializeLayout rather than in InitializeRow. The reasoning for this is that by doing this initially in InitializeRow, you will be potentially looping through all of your rows each time a new one is initialized, whereas this can be done once per “GroupBy” in InitializeLayout, and then you can hook InitializeRow to handle any value changes. This will potentially get around any possible performance issues with this behavior.

    I am attaching the sample project I used to test this. I hope it helps you.

    Please let me know if you have any other questions or concerns on this matter.

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
Clint Fisher
Favorites
0
Replies
6
Created On
Jun 22, 2020
Last Post
7 months, 2 weeks ago

Suggested Discussions

Tags

Created by

Created on

Jun 22, 2020 1:37 PM

Last activity on

Feb 19, 2026 12:38 PM