DataTable to observable collection

29,117

Solution 1

You don't want to create a new collection for each row in the table, but rather one collection for the entire table (with one object in the collection created for one row in the table):

var test = new ObservableCollection<Test>();
foreach(var row in TestTable.Rows)
{
    var obj = new Test()
    {
        id_test = (int)row["id_test"],
        name = (string)row["name"]
    };
    test.Add(obj);
}

Solution 2

I had a little issue with the accepted solution. It does not allow the [] brackets on type var.

var test = new ObservableCollection<Test>();
foreach(DataRow row in TestTable.Rows)
{
    test.Add(new Test()
    {
        id_test = (int)row["id_test"],
        name = (string)row["name"],
     });
 }
Share:
29,117

Related videos on Youtube

boo_boo_bear
Author by

boo_boo_bear

Updated on March 30, 2020

Comments

  • boo_boo_bear
    boo_boo_bear about 4 years

    I have been googling and searching for the answers here, but I still fail to understand a very basic thing - How to convert a DataTable to an Observable Collection?

    This is how far I've gotten:

    public ObservableCollection<Test> test;
    
    public class Test
    {
        public int id_test { get; set; }
        public string name { get; set; }
    } 
    

    Main..

     DataTable TestTable = new DataTable();
     TestTable.Columns.Add(new DataColumn("id_test", typeof(int)));
     TestTable.Columns.Add(new DataColumn("name", typeof(string)));
     DS.Tables.Add(TestTable);
    
    
    var test = new ObservableCollection<Test>();
            foreach(DataRow row in test_table.Rows)
         {
             var obj = new Test()
        {
            id_test = (int)row.ItemArray[0],
            name = (string)row.ItemArray[1]
    
        };
            test.Add(obj);
    

    I updated the code and it seems to be working.

  • boo_boo_bear
    boo_boo_bear about 11 years
    oh my, silly me you are right. But are you sure that the code above works? There is a huge chance that I have missed something, but 1)problem it doesn't recognize "name"
  • Andy
    Andy about 11 years
    @boo_boo_bear I tried to use the same column and property names as you did in your question - maybe there is a difference between the code in your project and what you originally posted? What exact error are you getting?
  • boo_boo_bear
    boo_boo_bear about 11 years
    Thank you for your help. I updated the code in the question, it's working. I know I shouldn't be asking other questions in comment section, but I have no idea how to bind it properly. It doesn't add anything to the ComboBoxColumn
  • Andy
    Andy about 11 years
    @boo_boo_bear yw :) You can always ask another question if you are encountering another problem.
  • lentz
    lentz about 6 years
    The semicolon after after the first variable assignment in new Test() needs to be a comma, and the semicolon after the second variable assignment needs to be removed.
  • lentz
    lentz about 6 years
    Remove comma after the last variable assignment in the new Test declaration.