Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: DataGrid
I'm trying to use the datagrid loadingrow event to color my row based on the value within the row. I have the following code within the loadingrow event handler:

DataRowView item = e.Row.Item as DataRowView;
if (item != null)
{
// my code to set the row background color
}

My problem is that the variable item is always null so my code never gets executed. I've used the debugger and when I view the "e.Row.Item" value, it does indeed show something besides "null." So, I'm at a loss for what would cause the assignment to result in "null" instead of the value I see as "e.Row.Item."
Can anyone shed any light on the problem and offer a solution?
I've inserted a "Console.WriteLine(e.Row.Item);" statement right after the above "if" statement and it indeed displays a value, not null!
Thanks in advance for the help.
Posted 19-Jan-12 12:54pm
Comments
Member 8192334 at 20-Jan-12 12:10pm
   
I don't believe that to be the case because eventually, the code would be executed at some point in time. I've placed a breakpoint inside the "if" block and the breakpoint is never reached...also, I've scrolled the data to force the datagrid to refresh itself and it still doesn't enter my "if" block...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

So, one more update to this puppy just to let folks know about new information that I found. In my LoadingRow event handler previously, I was setting the variable like this:
DataRowView item = e.Row.Item as DataRowView;
 
and then checking it's value, only to find that the value of item was never getting updated...
I did some more searching on the internet and found another "solution" that works in the LoadingRow event handler (in addition to the XAML trigger solution I previously posted). So, here's the "new" LoadingRow event handler code:
void dataGrid_LoadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)
{
    // Get the DataRow corresponding to the DataGridRow that is loading...
    OptionChain RowDataContext = e.Row.DataContext as OptionChain;
 
    if (RowDataContext != null)
    {
        if (RowDataContext.ochgdir == "Down")
            e.Row.Background = Brushes.Beige as Brush;
        else
            e.Row.Background = Brushes.GreenYellow as Brush;
    }
}
Note the difference in the new code is that I create a RowDataContext variable rather than a DataRowView variable. The resulting code is a bit simpler and IT WORKS! The DataGrid is bound to an observablecollection of "OptionChain" objects and ochgdir is a property of each of those objects.
I'm wondering if the problem with the original version might have something to do with one-way vs. two-way binding which, in effect, doesn't allow the assignment in my original LoadingRow event handler? If that's not the case, oh well, it's still a mystery!
Thanks.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The problem occurs probably because the LoadingRow event may actually fire several times. The Datagrid will only instantiate the row when it is needed and will recycle it when it goes out of view. Like when you start scrolling the grid.
 
See the remarks sections for details: http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.loadingrow(v=vs.95).aspx[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Well, I didn't actually solve the problem of why the assignment wasn't happening in my LoadingRow event handler, that's still a MYSTERY; but I did accomplish what I wanted another way. I modified my XAML to include the following code:

<pre lang="xml"><DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="TextElement.Foreground" Value="Green"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ochgdir}" Value="Down">
<Setter Property="TextElement.Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle></pre>

The result is that when the "ochgdir" property of the object bound to the DataGridRow contains the characters "Down" then the foreground color of the DataGrid row data is set to "Red" otherwise it's set to "Green."
Wonderful aspect of "programming" is that there's always more than one solution to a problem...it's just a matter of finding one that works for you! I DID!
Thanks to those who've pondered my post and to Ganesan for the reply.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 190
1 PIEBALDconsult 150
2 BillWoodruff 148
3 DamithSL 140
4 Jochen Arndt 125
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 20 Jan 2012
Copyright © CodeProject, 1999-2014
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