如何将DataTable转换成List<T>
4个回答
展开全部
动态生成的控件:
Private Sub UserControl1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim myPicture = New System.Windows.Forms.PictureBox()
Me.Panel3.Controls.Add(myPicture)
myPicture.Size = New System.Drawing.Size(115, 160)
myPicture.TabStop = False
myPicture.Name = "p"
myPicture.Cursor = Cursors.Hand
AddHandler myPicture.Click, AddressOf mypic_Click '添加事件
End Sub
处理事件的过程:
Private Sub mypic_Click() 'ByVal id As String
Dim bookInfo As New BookInfo
bookInfo.Show()
Me.Parent.Enabled = False
展开全部
DataTable dt = new DataTable("cart");
DataColumn dc1 = new DataColumn("tbl_name", Type.GetType("System.String")); DataColumn dc2 = new DataColumn("tbl_Id", Type.GetType("System.Int32"));
dt.Columns.Add(dc1); dt.Columns.Add(dc2);
foreach(对象 对象名 in list<对象>)
{
DataRow dr = dt.NewRow();
dr["prizename"] = 对象名.tbl_name.toString();
dr["point"] = Convert.toInt32(对象名.tbl_Id);
} //填充了10条相同的记录进去
DataColumn dc1 = new DataColumn("tbl_name", Type.GetType("System.String")); DataColumn dc2 = new DataColumn("tbl_Id", Type.GetType("System.Int32"));
dt.Columns.Add(dc1); dt.Columns.Add(dc2);
foreach(对象 对象名 in list<对象>)
{
DataRow dr = dt.NewRow();
dr["prizename"] = 对象名.tbl_name.toString();
dr["point"] = Convert.toInt32(对象名.tbl_Id);
} //填充了10条相同的记录进去
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
datatable转换为list的方法
public List<T> GetList<T>(DataTable table)
{
List<T> list = new List<T>();
T t = default(T);
PropertyInfo[] propertypes = null;
string tempName = string.Empty;
foreach (DataRow row in table.Rows)
{
t = Activator.CreateInstance<T>();
propertypes = t.GetType().GetProperties();
foreach (PropertyInfo pro in propertypes)
{
tempName = pro.Name;
if (table.Columns.Contains(tempName))
{
object value = row[tempName];
if (!value.ToString().Equals(""))
{
pro.SetValue(t, value, null);
}
}
}
list.Add(t);
}
return list.Count == 0 ? null : list;
}
public List<T> GetList<T>(DataTable table)
{
List<T> list = new List<T>();
T t = default(T);
PropertyInfo[] propertypes = null;
string tempName = string.Empty;
foreach (DataRow row in table.Rows)
{
t = Activator.CreateInstance<T>();
propertypes = t.GetType().GetProperties();
foreach (PropertyInfo pro in propertypes)
{
tempName = pro.Name;
if (table.Columns.Contains(tempName))
{
object value = row[tempName];
if (!value.ToString().Equals(""))
{
pro.SetValue(t, value, null);
}
}
}
list.Add(t);
}
return list.Count == 0 ? null : list;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
需要用实体类反射技术。有一些成型的类,可以找一下。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询