BinaryMee
BinaryMee

Reputation: 2142

How to change an IEnumerable collection so that it can be bound to a datagrid

I am developing an WPF application. It is having a DataGrid in it. I have assigned the ItemSource of my datagrid to an IEnumerable collection. I have a Treeview in my window. When I click the element in the treeview it has to load the datagrid

private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
       this.dataGrid1.ItemsSource = null;

       this.dataGrid1.Visibility = Visibility.Visible;
       this.dataGrid1.ItemsSource = objref.FinalValue;
       // Where Objref.FinalValue is an IEnumerable collection. 
       grid_data = objref.FinalValue;
}

But the problem is everytime the selection is changed, the values in the datagrid is not overwrittenen but it is appended. I flushed the datagrid with dataGrid1.Columns.Clear() and dataGrid.ItemSource = null; Later I found out that the objref.FinalValue is appended. So even though I flushed the datagrid it displays the entire value..

So in the class which is having objref as instance I have used

   private IEnumerable Result;

   public IEnumerable FinalValue
   {
       get { return Result; }
       set { Result = value; }
   }
   // Update Result with values so that it can be assigned to datagrid.

I need to overwrite not append. But the FinalValue has been appended every time. How can I resolve this issue?

Upvotes: 0

Views: 449

Answers (1)

Despertar
Despertar

Reputation: 22362

Whenever you update the ItemsSource after the grid is rendered you have to call dataGrid1.Items.Refresh() to update it. After calling Refresh() the datagrid rows will reflect the new collection that is bound to it.

Upvotes: 1

Related Questions