5 Replies - 357 Views - Last Post: 21 July 2014 - 05:40 AM Rate Topic: -----

#1 smadar_tsdaka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-November 12

DataGrid don't update after changing the ItemsSource

Posted 20 July 2014 - 05:26 AM

I have a data grid which I binded to a ObservableCollection object. On an event, I clear the ObservableCollection and add new items to it. When finished, I try to update the DataGrid, but it still shows the old rows. What am I doing wrong? This is my XAML:
<DataGrid 
   ItemsSource="{Binding }" 
   AutoGenerateColumns="False" 
   Name="dgvCurrentFaults" 
   TabIndex="0" 
   Background="Transparent" 
   RowBackground="#B4CDCD" 
   Foreground="#314E54" >
   <DataGrid.Columns>
      <DataGridTemplateColumn Header="Icon" Width="70" IsReadOnly="True">
         <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
               <Image Source="{Binding Icon}" Width="20" Height="20"/>
            </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
      <DataGridHyperlinkColumn  Header="Display" Binding="{Binding Display}" ContentBinding="{Binding Display}" IsReadOnly="True">
         <DataGridHyperlinkColumn.ElementStyle>
            <Style>
               <EventSetter Event="Hyperlink.Click" Handler="dgvCurrentFaults_CellContentClick"/>
            </Style>
         </DataGridHyperlinkColumn.ElementStyle>
      </DataGridHyperlinkColumn>
      <DataGridTextColumn Header="Fault Name" Binding="{Binding Falut_Name}" Width="150" IsReadOnly="True">
         <DataGridTextColumn.ElementStyle>
            <Style TargetType="TextBlock">
               <Setter Property="TextWrapping" Value="Wrap"/>
            </Style>
         </DataGridTextColumn.ElementStyle>
      </DataGridTextColumn>
      <DataGridTextColumn Header="Description" Binding="{Binding Fault_Description}" Width="240" IsReadOnly="True">
         <DataGridTextColumn.ElementStyle>
            <Style TargetType="TextBlock">
               <Setter Property="TextWrapping" Value="Wrap"/>
            </Style>
         </DataGridTextColumn.ElementStyle>
      </DataGridTextColumn>
      <DataGridTextColumn Header="Action Required" Binding="{Binding ActionRequired}" Width="200" IsReadOnly="True">
         <DataGridTextColumn.ElementStyle>
            <Style TargetType="TextBlock">
               <Setter Property="TextWrapping" Value="Wrap"/>
            </Style>
         </DataGridTextColumn.ElementStyle>
      </DataGridTextColumn>
      <DataGridTextColumn Header="ID Fault" Binding="{Binding IDFault}" Visibility="Hidden" IsReadOnly="True"/>
   </DataGrid.Columns>
</DataGrid>


and this is my code
public ObservableCollection<FaultsInfo> infoFaultList { get; set; }

private void UpdateTable()
{
    infoFaultList.Clear();
    infoFaultList.Add(new infoFault(1));
    infoFaultList.Add(new infoFault(2));

    dgvCurrentFaults.ItemsSource = null;
    dgvCurrentFaults.ItemsSource = infoFaultList;
    dgvCurrentFaults.UpdateLayout();
    dgvCurrentFaults.Items.Refresh();
}


Edit:

After having many more looks on the subject, I see that the first time I update the DataGrid is on the Loaded event of the UserControl. In this case, the DataGrid update fine. Later, the DataGrid updates on an event that is launched by some communication. In that case, it dose not udate. I thought that maybe the problem is that I try to update it from another thread, although I use Invoke.

This post has been edited by tlhIn`toq: 20 July 2014 - 07:06 AM
Reason for edit:: [code] YOUR CODE HERE [/code] tags added


Is This A Good Question/Topic? 0
  • +

Replies To: DataGrid don't update after changing the ItemsSource

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5523
  • View blog
  • Posts: 11,834
  • Joined: 02-June 10

Re: DataGrid don't update after changing the ItemsSource

Posted 20 July 2014 - 07:12 AM

To me, that's a screwy way to do binding.

Usually we do this

<DataGrid 
   ItemsSource="{Binding InfoFaultsList}" 
...


and are done with it. No need to do the .cs lines of .UpdateLayout() and .ItemsRefresh()
Its already an observable collection. You don't have to tickle it to get a reaction out of it.

There are several articles here on WPF if you check out the C# Learning series. I'm sure at least a couple cover binding.

C# Learning Series - tutorials here on DIC

Afterthought: At most you might need to raise the INotifyPropertyChanged event since you are updating the collection from another thread.
Was This Post Helpful? 0
  • +
  • -

#3 smadar_tsdaka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-November 12

Re: DataGrid don't update after changing the ItemsSource

Posted 20 July 2014 - 08:21 AM

Usually I would agree with you. I’ve used DataGrid on many occasions, and never faced such problems!
Yet, the fact is that I see the Items of the DataGrid update, but the data on screen dose not update…

This post has been edited by andrewsw: 20 July 2014 - 09:47 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,846
  • Joined: 12-December 12

Re: DataGrid don't update after changing the ItemsSource

Posted 20 July 2014 - 12:48 PM

Did you notice this additional point added by tlhIn`toq:

Quote

Afterthought: At most you might need to raise the INotifyPropertyChanged event since you are updating the collection from another thread.

Was This Post Helpful? 0
  • +
  • -

#5 smadar_tsdaka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-November 12

Re: DataGrid don't update after changing the ItemsSource

Posted 20 July 2014 - 11:15 PM

View Postandrewsw, on 20 July 2014 - 12:48 PM, said:

Did you notice this additional point added by tlhIn`toq:

Quote

Afterthought: At most you might need to raise the INotifyPropertyChanged event since you are updating the collection from another thread.


No I didnít! How do I do that?
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5523
  • View blog
  • Posts: 11,834
  • Joined: 02-June 10

Re: DataGrid don't update after changing the ItemsSource

Posted 21 July 2014 - 05:40 AM

Look in my signature block. There is a link to a tutorial on properties.

Don't be mad when I say if you are unfamiliar with basic properties you aren't ready to be designing an application from scratch. These foundtation concepts are well covered in any "Learn C# in 30 days" book. You need to work through a book cover to cover just doing the lessons and LEARNING before you can start developing on your own.

C# Learning Series - tutorials here on DIC
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1