Convert dataset to observable collection

12,909

Solution 1

A DataSet is a .Net representation of a set of tables, and the relationahips between them. It is kind of like an in-memory code-accessible representation of a mini-database. Only a few controls can bind directly to a dataset - those that are coded to analyze the relationships between the datasets tables and represent the various tables' data in some kind of hiearchical display, (like a treeview or a hiearchical grid) Anything that requires a simple list of items, with one or two properties for each item can not be bound directly to a dataste, it can only be bound to one of the contained datatables.

Alternatively, you need to dynamically construct and populate a datatable of your own, constructed from the various tables in the dataset you are using, to properly service the specific control you want to bind it to.

Solution 2

Here is how you convert a datatable to an observable collection:

  1. You need to create a class which contains properties. Each property represents a column in the datatable. Hence you need to set the types of properties as the types of columns in the datatable.
  2. Next you create a property in your View model with which you want to bind any control in xaml. This property would be of the type ObservableCollection. You can bind this property to a grid. In the case of Listbox you can make an ObservableCollection of strings and bind it to a listbox.
  3. You can directly populate you result from DB in Observable collection using LINQ, or alternatively you could manually add items in ObservableCollection from datatable.

There is no built-in function or cast with which you can convert a datatable to an observablecollection

Solution 3

Here is the code from Datatable to ObservabaleColleaction as @Hasan Fahim suggested...

        DataTable dtValues = new DataTable();
        dtValues.Columns.Add("Value1");
        dtValues.Columns.Add("Value2");
        dtValues.Columns.Add("Value3");
        dtValues.Columns.Add("Value4");

        DataRow dr = dtValues.NewRow();
        dr["Value1"] = "asad";
        dr["Value2"] = "naeeem";
        dtValues.Rows.Add(dr);           


 ObservableCollection Values = new ObservableCollection<MyClass>

 (dtValues.AsEnumerable().Select(i => new MyClass
      {

       Value1 = Convert.ToString(i["Value1"]),
       Value2 = Convert.ToString(i["Value2"]),
       Value3 = Convert.ToString(i["Value3"]),
       Value4 = Convert.ToString(i["Value4"])
  }));
Share:
12,909
biju
Author by

biju

Technology enthusiast.Currently fiddling with wpf

Updated on June 04, 2022

Comments

  • biju
    biju almost 2 years

    I was trying to bind a dataset to a listbox..certainly because i want to display a couple of tables information in a datatemplate..But this seems not possible and i will have to convert it to an observable collection.But how can i do it.My bl returns dataset objects.How can i convert this to observablecollection..? Is there any way so that i can handle this situation in MVVM..? How do people handle datasets in MVVM architecture..?