Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
115
Update cell background on value change for UltraGrid (data bound) using InitializeRow event
posted

Have been up all night trying to figure this one out. I am using an Infragistics.Win.UltraWinGrid.UltraGrid. The DataSource for the grid is a BindingSource that uses a custom data structure. Binding and property updates are working fine. Grid is readonly.

I want to change the color (toggle) of a row if the underlying value changes. Here is a very simplified snippet of my original code using the recommended InitializeRow event (note, for simplification I am just coloring the whole row for the example, whereas actually I am coloring a cell in my application):

private void ugWatchlist_InitializeRow(object sender, Infragistics.Win.UltraWinGrid.InitializeRowEventArgs e)
{
    if (!e.ReInitialize)
        return;

    Infragistics.Win.Appearance app1 = new Infragistics.Win.Appearance();
    app1.BackColor = Color.Green;
    Infragistics.Win.Appearance app2 = new Infragistics.Win.Appearance();
    app2.BackColor = Color.Red;

    if (e.Row.Appearance.BackColor.Name != Color.Green.Name)
        e.Row.Appearance = app1;
    else
        e.Row.Appearance = app2;

}


However, the color change works only once (and rows turn red on the firs t update and stay that way). According to the code, the color should toggle on each update. I can confirm in the debugger that the BackColor property is indeed changed but not reflected by the UltraGrid.

Am I missing something here? I have gone through all the samples and examples and couldn't find a solution to this problem. Please help.

Parents
  • 469350
    Verified Answer
    Offline posted

    Hi,

    I'm not sure why the color is only changing once, but the code you have here is not going to work in any case.

    You are making the assumption that the only time the InitializeRow event will fire is when something in the row has changed. That might be the case, but it's not really a good assumption to make. The event might fire for some other reason. For example, you could call grid.Rows.Refresh and force it to fire again for all rows.

    So your code here is essentially going to toggle the BackColor of the row at random.

    If you want to track rows that have changes in them, you need to come up with a better way of determining when a change occurs and what exactly you mean by this.

    What does it mean for a cell value to change? Change from what? When does it become "not changed" again? Does a change mean that the row has changes that have not yet been committed to the data source? Or does it mean that the DataSource has changes that have not been committed back to the database?

    Also... declaring new Appearance objects inside the InitializeRow event is incredibly wasteful and inefficient. You will be creating a huge number of identical objects and wasting massive amounts of memory. It would be better to create these two appearances inside the grid's InitializeLayout event and add them into the grid.DisplayLayout.Appearance collection and then just re-use the same two objects for as many rows (or cells) as you need.

     

Reply Children