Skip to main content

Microsoft Silverlight

Answered Question URGENT need help! Datagrid template column load event, strange behaviorRSS Feed

(0)

PrisonBreak
PrisonBreak

Member

Member

31 points

61 Posts

URGENT need help! Datagrid template column load event, strange behavior

 Hi SL masters,

I have a very strange problem and do not know the reason causes that.

 I have a datagrid in my program which has a template column and some other regular columns. I used the template column because according to the data it contains I want it to change its backcolor. So I used a button control in my template column and added a loaded event to this button you can see the datagrid code. I implemented the loaded event in my code, I am pasting the loaded event source code, too. Now the problem: When my datagrid first loads everthing is ok the color is changed according to the condition in the event code and it works fine. The problem arises when I begin to scroll on my datagrid. The first picture below is the picture of my datagrid's first load, everthing is fine. The second picture is the picture when I scroll on my datagrid to the bottom and come back to the top. Why after some scrolling the template column changes its color randomly I can not understand...

Please somebody help me I have a demo of my project tomorrow I have to fix this problem. Looking forward for help...

 

 


1               <my1:DataGrid x:Name="HedefDataGrid" AlternatingRowBackground="Lavender" VerticalScrollBarVisibility="Visible" 
2 AutoGenerateColumns="False" Width="auto" Height="auto" Grid.Row="0" Grid.Column="0"
3 Visibility="Collapsed" CanUserResizeColumns="False" SelectionChanged="HedefDataGrid_SelectionChanged"
4 IsReadOnly="True" ColumnHeaderStyle="{StaticResource DataGridStyle}">
5 <my1:DataGrid.Columns>
6 <my1:DataGridTextColumn Header="Alt Limit" DisplayMemberBinding="{Binding altLimit}"
7 Width="105" ElementStyle="{StaticResource DataGridTextColumnStyle}" />
8 <my1:DataGridTextColumn Header="Hedef" DisplayMemberBinding="{Binding hedefDeger}"
9 Width="70" ElementStyle="{StaticResource DataGridTextColumnStyle}" />
10 <my1:DataGridTextColumn Header="Üst Limit" DisplayMemberBinding="{Binding ustLimit}"
11 Width="105" ElementStyle="{StaticResource DataGridTextColumnStyle}" />
12
13 <my1:DataGridTemplateColumn>
14 <my1:DataGridTemplateColumn.Header>
15 <TextBlock Text="Skor" IsHitTestVisible="False" FontSize="14" Width="70"
16 TextWrapping="Wrap" Foreground="Black"/>
17 </my1:DataGridTemplateColumn.Header>
18 <my1:DataGridTemplateColumn.CellTemplate>
19 <DataTemplate>
20 <Button Content="{Binding skor}" Loaded="Score_Loaded" FontWeight="Bold"
21 TextWrapping="Wrap" Width="70" Foreground="Black"
22 VerticalContentAlignment="Center" HorizontalContentAlignment="Stretch"/>
23 </DataTemplate>
24 </my1:DataGridTemplateColumn.CellTemplate>
25 </my1:DataGridTemplateColumn>
26
27 <my1:DataGridTextColumn Header="Puan" DisplayMemberBinding="{Binding hedefPuan}"
28 Width="95" ElementStyle="{StaticResource DataGridTextColumnStyle}" />
29 <my1:DataGridTextColumn Header="Ağırlıklı Puan" DisplayMemberBinding="{Binding agirlikliHedefPuan}"
30 Width="115" ElementStyle="{StaticResource DataGridTextColumnStyle}" />
31 </my1:DataGrid.Columns>
32 </my1:DataGrid>
 
 
1        Private Sub Score_Loaded(ByVal sender As System.Object, ByVal e As System.EventArgs)
2
3 Dim columnSkor As Button = CType(sender, Button)
4 Dim hedef As HedefGostergeServiceReference.hedef = CType(columnSkor.DataContext, HedefGostergeServiceReference.hedef)
5
6 If hedef.ay = "12" AndAlso (Not (hedef.hedefDeger Is Nothing)) AndAlso (Not (hedef.skor Is Nothing)) AndAlso _
7 (Not (hedef.altLimit Is Nothing)) AndAlso (Not (hedef.ustLimit Is Nothing)) Then
8 Dim
ustLimitDouble As Double
9 Dim
altLimitDouble As Double
10 Dim
skorDouble As Double
11 Dim
temp As Double
12 Dim
switchScoreLabels As Integer = 0
13
14 If hedef.hedefDeger.Length = 10 AndAlso hedef.hedefDeger.Chars(2).Equals("."c) AndAlso hedef.hedefDeger.Chars(5).Equals("."c) Then
15 ustLimitDouble = handleDateFormat(hedef.ustLimit)
16 altLimitDouble = handleDateFormat(hedef.altLimit)
17 skorDouble = handleDateFormat(hedef.skor)
18 ElseIf (Double.TryParse(hedef.altLimit, temp) AndAlso Double.TryParse(hedef.ustLimit, temp) AndAlso Double.TryParse(hedef.hedefDeger, temp) _
19 AndAlso Double.TryParse(hedef.skor, temp) AndAlso Double.TryParse(hedef.agirlik, temp)) Then
20 ustLimitDouble = Double.Parse(hedef.ustLimit, New CultureInfo("tr-TR"))
21 altLimitDouble = Double.Parse(hedef.altLimit, New CultureInfo("tr-TR"))
22 skorDouble = Double.Parse(hedef.skor, New CultureInfo("tr-TR"))
23 Else
24 Exit Sub
25 End If
26
27 If
(altLimitDouble > ustLimitDouble) Then
28 switchScoreLabels = 1
29 Dim temp2 As Double = ustLimitDouble
30 ustLimitDouble = altLimitDouble
31 altLimitDouble = temp2
32 End If
33
34 If
skorDouble < altLimitDouble Then
35 If
switchScoreLabels = 1 Then
36 columnSkor.Background = New SolidColorBrush(Colors.Green)
37 Else
38 columnSkor.Background = New SolidColorBrush(Colors.Red)
39 End If
40 ElseIf
skorDouble > ustLimitDouble Then
41 If
switchScoreLabels = 1 Then
42 columnSkor.Background = New SolidColorBrush(Colors.Red)
43 Else
44 columnSkor.Background = New SolidColorBrush(Colors.Green)
45 End If
46 Else

47 columnSkor.Background = New SolidColorBrush(Colors.Yellow)
48 End If
49
50 End If
51 End Sub

   

simbuaarumugam
simbuaar...

Participant

Participant

1172 points

405 Posts

Re: URGENT need help! Datagrid template column load event, strange behavior

 

 Hi prison

You go through this link its fully have data grid functions 

Normal 0 false false false EN-US X-NONE X-NONE /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

http://blogs.msdn.com/scmorris/archive/2008/03/27/defining-columns-for-a-silverlight-datagrid.aspx

 

SimbuAarumugam India
http://simbusoftwareengineer.blogspot.com
(Mark As Answer If its Satisfy your needs)

PrisonBreak
PrisonBreak

Member

Member

31 points

61 Posts

Re: Re: URGENT need help! Datagrid template column load event, strange behavior

 Hi simbuaarumugam,

Thanks for the link but it does not tell the information related to the possible causes of my problem.

simbuaarumugam
simbuaar...

Participant

Participant

1172 points

405 Posts

Re: Re: Re: URGENT need help! Datagrid template column load event, strange behavior

 Hi Prison

Your code is wright but in my thought are u using any third party tool in your project 

SimbuAarumugam India
http://simbusoftwareengineer.blogspot.com
(Mark As Answer If its Satisfy your needs)

PrisonBreak
PrisonBreak

Member

Member

31 points

61 Posts

Re: Re: Re: URGENT need help! Datagrid template column load event, strange behavior

 No I dont use any tools.

 

The problem is that when I scroll the datagrid the event that I associated with my template column is raised again and again but it is not generating the correct color as it did in the first load.

 

Any workaround solutions? Time is ticking :( tomorrow it should be done...

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: URGENT need help! Datagrid template column load event, strange behavior

I can confirm your finding. I tried using Grid/Button/TextBlock as control in TemplateCell. Setting different background color or Fourground color to the control based on the row data. Everything works right when I first load the DataGrid. But as soon as I scroll the datagrid,  the color on those controls changes randomly. I think it must be a bug.

Could you report this in the Bug forum? 

OK I already appended this bug in one of the DataGrid bug thread in the Bug Forum: http://silverlight.net/forums/t/18436.aspx

 

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

PrisonBreak
PrisonBreak

Member

Member

31 points

61 Posts

Re: URGENT need help! Datagrid template column load event, strange behavior

 Hi sladapter, thanks for your answer. I think that should be a bug, too. By the way I put a break point to the button's load event and realized that when I scroll on the datagrid the event is raised randomly. As I know the rows of the datagrid is loaded at the beginning. Am I wrong? Why the loaded event is fired again and again and randomlyTongue Tied

So I want to ask you and the other experienced SL users: Is there a workaround solution to achieve this column color stuff? I said to the customer that this will be done, if I can't do that it will be really bad for me.


sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: URGENT need help! Datagrid template column load event, strange behavior

Yes, i saw that too. When I scroll the grid, the control loaded event fires redomely. But even that should not cause this rendom color change. Because the the data for those rows are still the same and so logic still stand the same as you first load. In my test, I did not even caught the loaded event fired by those  rows that color changed to a wrong color. So I have no clue why the color are changing.

I have not found a workaround. If I did, I would post. Can you limit your page size so you do not need to scroll?  

 

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

PrisonBreak
PrisonBreak

Member

Member

31 points

61 Posts

Re: URGENT need help! Datagrid template column load event, strange behavior

sladapter:
I have not found a workaround. If I did, I would post. Can you limit your page size so you do not need to scroll?  
 

 

It is not possible because we don't have any idea how many records will come from database.

 

By the way sladapter is there a way to deattach the load event from the template column after the grid completes its first load?

hwsoderlund
hwsoderlund

Member

Member

411 points

118 Posts

Re: URGENT need help! Datagrid template column load event, strange behavior

Just a thought about the random firing of Loaded: Isn't this due to the UI Virtualization built into the data grid? I.e. it doesn't load the actual rows until they should be displayed?

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: URGENT need help! Datagrid template column load event, strange behavior

 

hwsoderlund:

Just a thought about the random firing of Loaded: Isn't this due to the UI Virtualization built into the data grid? I.e. it doesn't load the actual rows until they should be displayed?

It could be. When I was trying to catch the loaded event, the rows fired event when I was scrolling seems the one that originally out of the viewing area. I could do more testing to confirm that.

PrisonBreak:

It is not possible because we don't have any idea how many records will come from database.

By the way sladapter is there a way to deattach the load event from the template column after the grid completes its first load?

I don't think detach the loaded event would fix the problem, because I can not even confirm that randomly change color is caused by the randomly fire loaded event.

The loaded event is fired per row. If the logic in the loaded event is correct, no matter how many times it runs that code, we should still get the same result. We should not see the result change unless the data is changed.

 

 

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

mingliangli
mingliangli

Member

Member

12 points

7 Posts

Re: URGENT need help! Datagrid template column load event, strange behavior

Look into this: http://leeontech.wordpress.com/2008/06/08/silverlight-datagrid-issues/ Maybe u need attach an unload event  

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Answered Question

Re: URGENT need help! Datagrid template column load event, strange behavior

Mingliangli,

Thank you for the link. Glad someone already report this.

According to YIF (don't know if he is from MS) this is not even a bug. It's caused by Virtualization. The row gets reused.

yif from lee's blog:

#2 This is not a bug either. Because of container recycling, the row gets reused for other items so the user needs to clean up any customizations from the Loading event in the UnLoading event. However, the recommended way to do this is to DataBind the customizations instead. In this example the Background could be bound to a calculated property of the item that returns a yellow brush if the City is London.

PrisonBreak,

Whether this is a bug it's debatable. but for now the most important thing is to find a workaround. 

yif suggested use DataBiding to bind the Background to another Brush field.  I thinkg you can bind the Background to the same Data field that you want to check, but use a IValueConverter to convert that data to a Brush object.

public class ValueConverter : IValueConverter
    {
      
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
          
           double data = value as double;         

           return new SolidColorBrush(Colors.Red);  // check data then return a SolidColorBrush based on the data
        }

 

Actually, after I wrote this,  I do think this is a better way to handle this than using the loaded event. Because in case you changed data,  loaded event won't be called automatically to switch your button color, but binding should take care it. I'll do this from now on.


 

 


sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

PrisonBreak
PrisonBreak

Member

Member

31 points

61 Posts

Re: Re: URGENT need help! Datagrid template column load event, strange behavior

 Hi sladapter thanks for your workaround, also Yi-Lun has posted solution code over the link:

http://silverlight.net/forums/t/27467.aspx

I implemented the solution as he suggested but I have a problem, I have written there Yi-Lun has not answered yet. Can you check it and help me please?

mahendra.gurav
mahendra...

Member

Member

136 points

158 Posts

Re: Re: Re: URGENT need help! Datagrid template column load event, strange behavior

Hi everyone,

  I m having the same datgrid scrooling issue. i used SL 2 beta 2.

Plz give any solution over this.

 

Thanks,

Mahendra

PrisonBreak
PrisonBreak

Member

Member

31 points

61 Posts

Re: Re: Re: URGENT need help! Datagrid template column load event, strange behavior

 Hi, check this thread http://silverlight.net/forums/t/27467.aspx

 

Yi-Lun's converter worked for me when using beta 2. I do not know the row load event ,is still acting like beta 2 or not because today I upgraded from beta 2 to RC1 and I did not try it yet. Try to do that in RC1 if still problem exists follow the solution in my link. If you have any problems with that solution let me know.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities