Click here to Skip to main content
13,044,428 members (102,774 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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:
<br />
DataRowView item = e.Row.Item as DataRowView;<br />
if (item != null)<br />
{<br />
   // my code to set the row background color<br />
}<br />

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 11:54am
Member 8192334 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
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:
<br />
<pre lang="cs">void dataGrid_LoadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)<br />
{<br />
    // Get the DataRow corresponding to the DataGridRow that is loading...<br />
    OptionChain RowDataContext = e.Row.DataContext as OptionChain;<br />
<br />
    if (RowDataContext != null)<br />
    {<br />
        if (RowDataContext.ochgdir == "Down")<br />
            e.Row.Background = Brushes.Beige as Brush;<br />
        else<br />
            e.Row.Background = Brushes.GreenYellow as Brush;<br />
    }<br />
}</pre><br />
<br />

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!
Rate this: bad
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:[^]
Rate this: bad
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:
<br />
<pre lang="xml"><DataGrid.RowStyle><br />
     <Style TargetType="DataGridRow"><br />
         <Setter Property="TextElement.Foreground" Value="Green"/><br />
         <Style.Triggers><br />
             <DataTrigger Binding="{Binding ochgdir}" Value="Down"><br />
                 <Setter Property="TextElement.Foreground" Value="Red"/><br />
             </DataTrigger><br />
         </Style.Triggers><br />
     </Style><br />
 </DataGrid.RowStyle></pre><br />

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'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.

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 | Mobile
Web02 | 2.8.170713.1 | Last Updated 20 Jan 2012
Copyright © CodeProject, 1999-2017
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