Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

say, i have an object layer

C#
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

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


or when i add data row, this should be

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


NOTE: i am not going to use Entity Data Model.
Posted
Updated 16-Dec-12 2:31am
v3
Comments
Jibesh 16-Dec-12 8:38am    
why would you need a DataTable then? cant you use any other collection class, generic class etc?

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:

C#
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]);
}
 
Share this answer
 
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[^]
 
Share this answer
 
v4
Comments
Faisalabadians 16-Dec-12 8:53am    
dear, please see "User" is a type which has its own properties. these properties should becom column in datatable, and object of User should be added as datarow
Jibesh 16-Dec-12 8:58am    
User has its own properties but these properties never gets added as column of the tables as if the Table Contains only one columns that is User. Unless you override the ToString() of User class if you load the DataTable in gridview or any other User Interface it will be displaying class name in the first column. no other data or column will be displayed or added to the DataTable
Faisalabadians 16-Dec-12 9:00am    
is it possible to build a user defined datatable?
Jibesh 16-Dec-12 9:05am    
If you want to add anything additional to the native class you are always welcome to override.

try deriving a class from DataRow to meet your request but must be careful you override/add all the methods in proper way so that consumer of this class use it without too much work.
Faisalabadians 16-Dec-12 9:07am    
can u please tell me, how can i derive my class to datatable class? and which methods are requried to be overrieden to behave as i want?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900