Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
5.00/5 (1 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
Comments
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...

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:
C#
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.
 
Share this answer
 
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[^]
 
Share this answer
 
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 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.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900