自定义DataTable以将客户对象层保存为数据 [英] Customizing DataTable to hold custome object layer as data

查看:67
本文介绍了自定义DataTable以将客户对象层保存为数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





说,我有一个对象层



  public   class 用户
{
public int ID { get ; set ;}
public string FirstName {获取; 设置;}
public string LastName { get ; set ;}
< span class =code-keyword> public string 地址{ get ; set ;}
}





现在我希望数据表必须包含用户对象作为一个类。当我使用数据表数据行时,它必须返回User对象作为datarow



喜欢这个



用户user =  new  User(); 
user = DataTable.rows [ 2 ];





或者当我添加数据行时,这应该是



 datatable.rows。 add  new  User()); 





注意:我我不打算使用实体数据模型。

解决方案

我认为重新输入DataRow作为用户是不可能的,你必须单独填写每个属性,或者你可以使用一些实体您不想要的框架,或者您可以在User中拥有两个构造函数并将其填充为一行,如下所示:



 < span class =code-keyword> public   class 用户
{
public int ID { get ; set ;}
public string FirstName { get ; set ;}
public string LastName { get ; set ;}
public string 地址{ get ; set ;}

public 用户()
{
}
public 用户( int Id, string FirstName, string LastName, string 地址)
{
.Id = Id;
this .FirstName = FirstName;
this .LastName = LastName;
this .Address = Address;
}
}
public void SomeMethod()
{
用户user = 用户(DataTable.Rows [ 2 ] [ 0 ],DataTable.Rows [ 2 ] [ 1 ],DataTable.Rows [ 2 ] [ 2 ],DataTable.Rows [ 2 ] [ 3 ]);
}


是的。 DataTable可以在其列中存储用户定义的对象。



像其他原生类型一样使用它

 DataTable dt = new dataTable(); 
dt.Columns.Add(new DataColumn(User,typeof(User));

DataRow row = dt.NewRow();
row [User] = new User();

dt.Rows.Add(row);

用户usr = dt.Rows [0] [User] as User;





/ EDIT

提供列名以检索数据

/ EDIT



如何覆盖添加的DataTable和DataRow。在OPs评论之后。



how-to-extend-datarow-and-datatable -in-c-sharp-with-additional-properties-and-me [ ^ ]

不确定如何继承DataRow正确 [ ^ ]


hi,

say, i have an object layer

public class User
{
	public int Id{get;set;}
	public string FirstName{get;set;}
	public string LastName{get;set;}
	public string Address{get;set;}
}



now i want that datatable must contain User objects as a class. when i use datatable data row, it must return User object as datarow

like this

User user = new User();
user = DataTable.rows[2];



or when i add data row, this should be

datatable.rows.add(new User());



NOTE: i am not going to use Entity Data Model.

解决方案

I think it''s imposible retype DataRow as User, you must fill each property separately or you can use some Entity Framework which you don''t want or you can have two constructors in User and fill it in one row like this:

public class User
{
    public int Id{get;set;}
    public string FirstName{get;set;}
    public string LastName{get;set;}
    public string Address{get;set;}
    
    public User()
    {
    }
    public User(int Id, string FirstName, string LastName, string Address)
    {
        this.Id = Id;
        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Address = Address;
    }
}
public void SomeMethod()
{
    User user = new User (DataTable.Rows[2][0], DataTable.Rows[2][1], DataTable.Rows[2][2], DataTable.Rows[2][3]);
}


Yes. DataTable can store user defined objects in its columns.

use it like the other native types

DataTable dt = new dataTable();
dt.Columns.Add(new DataColumn("User",typeof(User));

DataRow row = dt.NewRow();
row["User"] = new User();

dt.Rows.Add(row);

User usr = dt.Rows[0]["User"] as User; 



/EDIT
Provide the Column Name to retrieve the data
/EDIT

How to override DataTable and DataRow added. After OPs Comment.

how-to-extend-datarow-and-datatable-in-c-sharp-with-additional-properties-and-me[^]
Not sure how to inherit DataRow correctl[^]


这篇关于自定义DataTable以将客户对象层保存为数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆