Convert DataRow to Object

I created a general list and populated some objects. Then, the list mentioned above was converted to a DataTable for use in a DataGridView. The problem is when I want to get a row from this grid, I have a DataRow. I wanted to convert this to my object again, but I don’t know how to do it. Maybe you could give an example?

thanks

+3
source share
4 answers

Well, if you cannot or will not use ORM (an object-relational resolver, for example Linq-to-SQL or NHibernate), which these tools do well for you), you will have to do it yourself.

DataRow - , :

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;
}

, - ( ) (, NULL ..).

, , DataRow as .

+11

, MyObject, :

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

- :

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();
+9

BindingList < > , List < > ? DataTable . , INotifyPropertyChanged , BindingList, datagrid .

BindingList < > - - .

+1

, ORM. , , , :

public static class MapperExtensionClass
{

        public static IEnumerable<MyClassType> ToMyClassTypeEnumerable(this DataTable table)
        {
            return table.AsEnumerable().Select(r => r.ToMyClassType());
        }

        public static MyClassType ToMyClassType(this DataRow row)
        {            
            return row.ToObject<MyClassType>();
        }

        public static T ToObject<T>(this DataRow row) where T: new()
        {
            T obj = new T();
            foreach (PropertyInfo property in typeof(T).GetProperties())
            {
                if (row.Table.Columns.Contains(property.Name))
                {
                    property.SetValue(obj, property.PropertyType.ToDefault(row[property.Name]));
                }
            }

            return obj;
        }


        public static object ToDefault(this Type type, object obj)
        {
            if (type == null)
                throw new Exception("Customized exception message");

            var method = typeof(MapperExtensionClass)
                .GetMethod("ToDefaultGeneric", BindingFlags.Static | BindingFlags.Public);

            var generic = method.MakeGenericMethod(type);

            return generic.Invoke(null, new object[] { obj });            
        }

        public static T ToDefaultGeneric<T>(object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return default(T); 
            }
            else
            {
                return (T)obj;
            }
        }
}

, GridView . , , .

0

Source: https://habr.com/ru/post/1722480/


All Articles