Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

URGENT need help! Datagrid template column load event,... RSS

20 replies

Last post Jun 15, 2011 03:16 PM by sladapter

(0)
  • PrisonBreak

    PrisonBreak

    Member

    31 Points

    61 Posts

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

    Sep 17, 2008 10:06 AM | LINK

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

     

     


               <my1:DataGrid x:Name="HedefDataGrid" AlternatingRowBackground="Lavender" VerticalScrollBarVisibility="Visible" 
    AutoGenerateColumns="False" Width="auto" Height="auto" Grid.Row="0" Grid.Column="0"
    Visibility="Collapsed" CanUserResizeColumns="False" SelectionChanged="HedefDataGrid_SelectionChanged"
    IsReadOnly="True" ColumnHeaderStyle="{StaticResource DataGridStyle}">
    <my1:DataGrid.Columns>
    <my1:DataGridTextColumn Header="Alt Limit" DisplayMemberBinding="{Binding altLimit}"
    Width="105" ElementStyle="{StaticResource DataGridTextColumnStyle}" />
    <my1:DataGridTextColumn Header="Hedef" DisplayMemberBinding="{Binding hedefDeger}"
    Width="70" ElementStyle="{StaticResource DataGridTextColumnStyle}" />
    <my1:DataGridTextColumn Header="Üst Limit" DisplayMemberBinding="{Binding ustLimit}"
    Width="105" ElementStyle="{StaticResource DataGridTextColumnStyle}" />

    <my1:DataGridTemplateColumn>
    <my1:DataGridTemplateColumn.Header>
    <TextBlock Text="Skor" IsHitTestVisible="False" FontSize="14" Width="70"
    TextWrapping="Wrap" Foreground="Black"/>
    </my1:DataGridTemplateColumn.Header>
    <my1:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
    <Button Content="{Binding skor}" Loaded="Score_Loaded" FontWeight="Bold"
    TextWrapping="Wrap" Width="70" Foreground="Black"
    VerticalContentAlignment="Center" HorizontalContentAlignment="Stretch"/>
    </DataTemplate>
    </my1:DataGridTemplateColumn.CellTemplate>
    </my1:DataGridTemplateColumn>

    <my1:DataGridTextColumn Header="Puan" DisplayMemberBinding="{Binding hedefPuan}"
    Width="95" ElementStyle="{StaticResource DataGridTextColumnStyle}" />
    <my1:DataGridTextColumn Header="Ağırlıklı Puan" DisplayMemberBinding="{Binding agirlikliHedefPuan}"
    Width="115" ElementStyle="{StaticResource DataGridTextColumnStyle}" />
    </my1:DataGrid.Columns>
    </my1:DataGrid>
     
     
        Private Sub Score_Loaded(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Dim columnSkor As Button = CType(sender, Button)
    Dim hedef As HedefGostergeServiceReference.hedef = CType(columnSkor.DataContext, HedefGostergeServiceReference.hedef)

    If hedef.ay = "12" AndAlso (Not (hedef.hedefDeger Is Nothing)) AndAlso (Not (hedef.skor Is Nothing)) AndAlso _
    (Not (hedef.altLimit Is Nothing)) AndAlso (Not (hedef.ustLimit Is Nothing)) Then
    Dim
    ustLimitDouble As Double
    Dim
    altLimitDouble As Double
    Dim
    skorDouble As Double
    Dim
    temp As Double
    Dim
    switchScoreLabels As Integer = 0

    If hedef.hedefDeger.Length = 10 AndAlso hedef.hedefDeger.Chars(2).Equals("."c) AndAlso hedef.hedefDeger.Chars(5).Equals("."c) Then
    ustLimitDouble = handleDateFormat(hedef.ustLimit)
    altLimitDouble = handleDateFormat(hedef.altLimit)
    skorDouble = handleDateFormat(hedef.skor)
    ElseIf (Double.TryParse(hedef.altLimit, temp) AndAlso Double.TryParse(hedef.ustLimit, temp) AndAlso Double.TryParse(hedef.hedefDeger, temp) _
    AndAlso Double.TryParse(hedef.skor, temp) AndAlso Double.TryParse(hedef.agirlik, temp)) Then
    ustLimitDouble = Double.Parse(hedef.ustLimit, New CultureInfo("tr-TR"))
    altLimitDouble = Double.Parse(hedef.altLimit, New CultureInfo("tr-TR"))
    skorDouble = Double.Parse(hedef.skor, New CultureInfo("tr-TR"))
    Else
    Exit Sub
    End If

    If
    (altLimitDouble > ustLimitDouble) Then
    switchScoreLabels = 1
    Dim temp2 As Double = ustLimitDouble
    ustLimitDouble = altLimitDouble
    altLimitDouble = temp2
    End If

    If
    skorDouble < altLimitDouble Then
    If
    switchScoreLabels = 1 Then
    columnSkor.Background = New SolidColorBrush(Colors.Green)
    Else
    columnSkor.Background = New SolidColorBrush(Colors.Red)
    End If
    ElseIf
    skorDouble > ustLimitDouble Then
    If
    switchScoreLabels = 1 Then
    columnSkor.Background = New SolidColorBrush(Colors.Red)
    Else
    columnSkor.Background = New SolidColorBrush(Colors.Green)
    End If
    Else

    columnSkor.Background = New SolidColorBrush(Colors.Yellow)
    End If

    End If
    End Sub

       
  • simbuaarumugam

    simbuaarumugam

    Participant

    1182 Points

    405 Posts

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

    Sep 17, 2008 11:56 AM | LINK

     

     Hi prison

    You go through this link its fully have data grid functions 

    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

    31 Points

    61 Posts

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

    Sep 17, 2008 12:04 PM | LINK

     Hi simbuaarumugam,

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

  • simbuaarumugam

    simbuaarumugam

    Participant

    1182 Points

    405 Posts

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

    Sep 17, 2008 01:49 PM | LINK

     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

    31 Points

    61 Posts

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

    Sep 17, 2008 02:30 PM | LINK

     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

    43607 Points

    7907 Posts

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

    Sep 17, 2008 03:45 PM | LINK

    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

     

     

    Sally Xu
    Software Engineer
    Aprimo, Inc

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

    PrisonBreak

    Member

    31 Points

    61 Posts

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

    Sep 18, 2008 06:04 AM | LINK

     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 randomly[:S]

    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

    43607 Points

    7907 Posts

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

    Sep 18, 2008 02:28 PM | LINK

    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?  

     

     

    Sally Xu
    Software Engineer
    Aprimo, Inc

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

    PrisonBreak

    Member

    31 Points

    61 Posts

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

    Sep 18, 2008 02:46 PM | LINK

    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

    429 Points

    123 Posts

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

    Sep 18, 2008 02:54 PM | LINK

    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?