tags:

views:

998

answers:

3

Hi, I've created generic List and populate with some objects. Then List I mentioned before converted into DataTable to use in DataGridView. Problem is when I want get Row from this grid I have DataRow. I wanted to convert this to my object againt but not sure how to do it. Maybe you could give some example?

Thanks

A: 

Assuming you're using a class MyObject, defined as follows :

class MyObject
{
    public string Foo { get; set; }
    public int Foo { get; set; }
}

You could do something like that :

using System.Data.DataSetExtensions;

...

List<MyObject> list = (from row in table.AsEnumerable()
                       select new MyObject
                       {
                            Foo = row.Field<string>("foo"),
                            Bar = row.Field<int>("bar")
                       }).ToList();
Thomas Levesque
+3  A: 

Well, if you can't or won't use an "ORM" (object-relational mapper, like Linq-to-SQL or NHibernate - that's exactly what these tools do, and do quite well for you), you'll have to do this yourself.

Converting a DataRow into a domain object model is pretty boring code, really:

public Customer ConvertRowToCustomer(DataRow row)
{
   Customer result = new Customer();

   result.ID = row.Field<int>("ID");
   result.Name = row.Field<string>("CustomerName");
   ..... // and so on

   return result;
}

The biggest challenge here is making this rock-solid and handling (or avoiding) all possible errors (like a field being NULL etc.).

Another possibility would be to have a constructor on your domain model object type that would take a DataRow as parameter and construct a new object from it.

Marc

marc_s
+1 for suggesting an ORM.
Darin Dimitrov
Thanks,Can I use this(of course with some changes) when I have many types to convert to. For example one DataTable has records with Customer data and other records with addresses? Do I have to use reflection ?
arek
You could of course use reflection to generalize this method for any type, and to even further improve the degraded performance resulting from reflection you could use Reflection.Emit to generate dynamic assemblies and then add some lazy loading to your collections and at the end of the day you will understand that people have already taught about these problems and invented ORM such as NHibernate , Entity Framework, ... and you will realize that you've put efforts into something that already exists and that you could use for free.
Darin Dimitrov
Maybe I should explain why I don't use EF and other ORMs. I get some array of objects from WS and then populate grid with these items. Only reason why I convert this array into DataTable is that I wanted to sort and found many examples that converting into DataTable is good way to provide this.
arek
Sorting of in-memory arrays is done pretty easy with Linq-to-Objects.
Darin Dimitrov
Well, I mean I wanted give user chance to sort data in grid. When I populate gird with array I wasn't able to do this
arek
A: 

Why not just put your objects into a BindingList<> rather than a List<>? Then you can skip the converting to DataTable and back again exercise. You may need to implement INotifyPropertyChanged on your objects, but once they are inside a BindingList, changes in the datagrid will automatically be applied to your underlying objects.

Sorting can be handled by either sorting the list manually on column header click, or by inheriting from BindingList<> and implementing the sorting functionality inside it - then clicking on a header automatically sorts the list - no code required.

Zack