1

I have a class "Seive", a ObservableCollection seiveData object.

        public Seive(String id)
    {
        SeiveIdSize = id;
        Caret = 0.0;
        Percent = 0.0;
        Peices = 0;
        Weight = 0.0;
        Size = 0;
    }

    public String SeiveIdSize   {    get;   set;   }
    public Double Weight { get; set; }
    public Double Percent   {    get;   set;   }
    public Double Caret {    get;   set;   }
    public uint Size    {    get;   set;   }
    public uint Peices  {    get;   set;   }

In my xml : I have

<DataGrid  Name="serverGrid" ItemsSource="{Binding}" .....>
<DataGrid.Columns>
<DataGridTextColumn Header="SEIVE" Width="Auto" Binding="{Binding Path=SeiveIdSize}" SortDirection="Ascending" />
 <DataGridTextColumn Header="CTS" Width="Auto" Binding="{Binding Path=Caret}" />
 <DataGridTextColumn Header=" % " Width="Auto" Binding="{Binding Path=Percent}" />
 <DataGridTextColumn Header="PCS" Width="Auto" Binding="{Binding Path=Peices}" />
 <DataGridTextColumn Header="WGT" Width="Auto" Binding="{Binding Path=Weight}" />
 <DataGridTextColumn Header="SIZE" Width="Auto" Binding="{Binding Path=Size}" />                                    
 </DataGrid.Columns>

In window Loaded event, I fill 2 seives in seiveData, yet I don't see any results/rows.

seivesData.Add(new Seive("+10"));
seivesData.Add(new Seive("+8"));
seivesDataGrid.DataContext = seivesData;

EDIT : Button Event Code :

        private void saveBtn_Click(object sender, RoutedEventArgs e)
    {
        Seive s1 = new Seive("+2");
        s1.Peices = 100;
        s1.Caret = 0.41;
        s1.Weight = 0.10;
        seivesData.Add(s1);
        Seive s = seivesData[0];
        s.Caret = 0.54;
        s.Weight = 0.32;
        seivesData[0] = s;
        seiveDG.DataContext = seivesData;
    }

Where am I going wrong ? I can see the newly added Seive all details, but Not the Caret & Weight added to 0th seive.

1
  • Ever heard of INotifyPropertyChanged? Very important interface for such requirements. Commented May 17, 2012 at 11:17

4 Answers 4

2

The basic problem i see in your xaml is that you set the ItemsSource = {binding SeiveData} which is wrong if your pass this property into data context of the grid.

This below code is working now. check it.

one more thing if your want to notify the chagnes into the class Seive then must implement the INotifyPropertyChange interface.

XAML

<DataGrid Name="serverGrid" ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTextColumn Width="Auto"
                                Binding="{Binding Path=SeiveIdSize}"
                                Header="SEIVE"
                                SortDirection="Ascending" />
            <DataGridTextColumn Width="Auto"
                                Binding="{Binding Path=Caret}"
                                Header="CTS" />
            <DataGridTextColumn Width="Auto"
                                Binding="{Binding Path=Percent}"
                                Header=" % " />
            <DataGridTextColumn Width="Auto"
                                Binding="{Binding Path=Peices}"
                                Header="PCS" />
            <DataGridTextColumn Width="Auto"
                                Binding="{Binding Path=Weight}"
                                Header="WGT" />
            <DataGridTextColumn Width="Auto"
                                Binding="{Binding Path=Size}"
                                Header="SIZE" />
        </DataGrid.Columns>
    </DataGrid> 

Cose behind

 ObservableCollection<Seive> _seiveData;
        public ObservableCollection<Seive> SeiveData
        {
            get { return _seiveData; }
            set { _seiveData = value; }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            SeiveData = new ObservableCollection<Seive>();
            SeiveData.Add(new Seive("+10"));
            SeiveData.Add(new Seive("+8"));

            serverGrid.DataContext = SeiveData;

        }

class

 public class Seive
    {
        public Seive(String id)
        {
            SeiveIdSize = id;
            Caret = 0.0;
            Percent = 0.0;
            Peices = 0;
            Weight = 0.0;
            Size = 0;
        }

        public String SeiveIdSize { get; set; }
        public Double Weight { get; set; }
        public Double Percent { get; set; }
        public Double Caret { get; set; }
        public uint Size { get; set; }
        public uint Peices { get; set; }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I implemented INotifyPropertyChange in my Seive class, and it worked. Thanks once again.
1

You mention you're using a list, but for databinding to work, you would need to use ObservableCollection<T> which will notify the datagrid if the collection changes.

EDIT:

As pointed out in the comments, it's not about changes. You're already setting your datacontext to the list, so there won't be a seiveData in your DataContext. Instead, try the following:

ItemsSource="{Binding}"

If you still need to notify your view of changes, consider ObservableCollection<T> as has been mentioned.

5 Comments

but the question is not about changes, question is about populating the data grid.
I changed List to ObservableCollection, but no difference.
In Loaded event, I replaced Datacontext with ItemSource as you mentioend and yes I can see the data. But my point is not just ot add, I might add/change dynamically also. In a btn click, I added a new Seive and changed some values of 0th seive & called .DataContenxt = seivesData;. The newly added seive was updated/seen in the datagrid, but the updated values of 0th object was not refreshed. Why so ?
I did not suggest to set the ItemsSource from code, but I suggested to change your binding in XAML. Either set the DataContext from code and use ItemsSource="{Binding}" or set the ItemsSource from code, don't mix them up. For refreshing, ObservableCollection<T> should suffice.
Ok as u suggested, in xml i have ItemsSource="{Binding}" and in Loaded and in btn event after adding& changing values I call Datacontext = seivesData. But yet, the results are same i.e. newly added is shown, but updated values of existing object is not refreshed. My seivesData is also of ObservableCollection<Seive> seivesData; data type. Have shown the btn event code in my question. May be that might help you support me. Thanks.
0

Probably the DataContext of the container, the DataGrid is in, is not set to the object that serves the seiveData-property. Or maybe your seiveData-list is not even backed by a property but is only a field. This is not allowed. Even if the field (member variable) is declared as public, the binding engine will not bind to it - it must be backed by a property.

Pleasee notice also, if you fill the list in Loaded-event, seiveList must be an INotifyCollectionChanged-implementing collection such as ObservableCollection<T> because the binding is done before the population of the list. Otherwise, the DataGrid will not known that new data has been inserted into the list.

Comments

0

One way as every one stated is to bind it to ObservableCollection.

Another way is to bind from DataTable.

Convert your List to DataTable using

How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow

Creating a DataTable From a Query (LINQ to DataSet)

I found this today. Very Effective.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.