Reflection extension method to bind DataRow values to object properties.

/ Published in: Visual Basic
Save to your folder(s)

This will populate object properties with values from a DataRow. The DataTable column names must match the property names and types of the target class.


DataTable dt = new DataTable();
dt.Columns.Add("TestString", typeof(string));
dt.Columns.Add("TestBool", typeof(bool));
dt.Columns.Add("TestInt", typeof(int));
dt.Columns.Add("TestDouble", typeof(double));
dt.Columns.Add("TestDecimal", typeof(decimal));
dt.Columns.Add("TestFloat", typeof(string));

DataRow dr = dt.NewRow();
dr["TestString"] = "Hello";
dr["TestBool"] = false;
dr["TestInt"] = DBNull.Value;
dr["TestDouble"] =4.56;
dr["TestDecimal"] = 7.89m;
dr["TestFloat"] = "3.14f";

TestClass tc = new TestClass();

class TestClass
public string TestString { get; set; }
public bool? TestBool { get; set; }
public int? TestInt { get; set; }
public double TestDouble { get; set; }
public decimal TestDecimal { get; set; }
public float TestFloat { get; set; }

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.