-
May 21st, 2010, 01:32 AM
#1
Convert Generic List To Dataset
Can anyone teach me how to convert the listobj to dataset?
Code:
ROLES_FORMSEN argEn = new ROLES_FORMSEN();
ROLES_FORMS dalObj = new ROLES_FORMS();
List<ROLES_FORMSEN> listObj = dalObj.GetList(argEn);
GridView1.DataSource = listObj;
GridView1.DataBind();
-
May 21st, 2010, 02:45 AM
#2
Re: Convert Generic List To Dataset
Originally Posted by daniel50096230
Can anyone teach me how to convert the listobj to dataset?...
A List<something> never can be a Dataset !
Dataset is a collection of DataTables so you cannot convert it that way.
If you want to bind data to a Grid then simple fill a Dataset directly from the database using DataAdapter. Or if needed create a DataTable and fill it by use of code with all the data of your List.
This DataTable you can bind to your Grid
Last edited by JonnyPoet; May 21st, 2010 at 03:52 AM.
Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 21st, 2010, 03:02 AM
#3
Re: Convert Generic List To Dataset
Do you have any sample on fill the data from list into datatable??Because I am not very good in datatable..
-
May 21st, 2010, 03:52 AM
#4
Re: Convert Generic List To Dataset
Originally Posted by daniel50096230
Do you have any sample on fill the data from list into datatable??Because I am not very good in datatable..
You can use a foreach loop to go through the list
At first you need to create a DataTable which contains each of the columns your gridview should contain. This needs to be exactly defined just like a table in a database name of Column, type of data... then in your loop you simple add DataRows to your DataTable by filling each Row with the needed Values.
There are lots of DataTable examples here in the forum IMHO. Simple search for DataTable
Here is one for creation of a DataTable
http://dotnetperls.com/datatable-use
Last edited by JonnyPoet; May 21st, 2010 at 03:55 AM.
Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 21st, 2010, 06:23 AM
#5
Re: Convert Generic List To Dataset
Hi,
There is an way to convert a generic collection to datatable. With this you can populate a datatable with columns (properties of the collection) and rows (values of the properties).
Here is the code to do the same.
public static DataTable ConvertTo<T>(IList<T> genericList)
{
//create DataTable Structure
DataTable dataTable = CreateTable<T>();
Type entType = typeof(T);
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
//get the list item and add into the list
foreach (T item in genericList)
{
DataRow row = dataTable.NewRow();
foreach (PropertyDescriptor prop in properties)
{
row[prop.Name] = prop.GetValue(item);
}
dataTable.Rows.Add(row);
}
return dataTable;
}
public static DataTable CreateTable<T>()
{
//T –> ClassName
Type entType = typeof(T);
//set the datatable name as class name
DataTable dataTable = new DataTable(entType.Name);
//get the property list
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
foreach (PropertyDescriptor prop in properties)
{
//add property as column
dataTable.Columns.Add(prop.Name, prop.PropertyType);
}
return dataTable;
}
Now with this code you can convert any generic list to a datatable.
Regards,
Jayashree K R.
-
May 21st, 2010, 11:41 AM
#6
Re: Convert Generic List To Dataset
Originally Posted by contactshree
Hi,
There is an way to convert a generic collection to datatable. .....
Great. seems to be what I have tried to describe. But please can you edit this post and use codetags so the code stays correctly formatted and easy to be read ?
Last edited by JonnyPoet; May 21st, 2010 at 11:45 AM.
Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 24th, 2010, 01:56 AM
#7
Re: Convert Generic List To Dataset
Thanks for the reply. This is my first post, I missed it.
Code:
public static DataTable ConvertTo<T>(IList<T> genericList)
{
//create DataTable Structure
DataTable dataTable = CreateTable<T>();
Type entType = typeof(T);
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
//get the list item and add into the list
foreach (T item in genericList)
{
DataRow row = dataTable.NewRow();
foreach (PropertyDescriptor prop in properties)
{
row[prop.Name] = prop.GetValue(item);
}
dataTable.Rows.Add(row);
}
return dataTable;
}
public static DataTable CreateTable<T>()
{
//T –> ClassName
Type entType = typeof(T);
//set the datatable name as class name
DataTable dataTable = new DataTable(entType.Name);
//get the property list
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
foreach (PropertyDescriptor prop in properties)
{
//add property as column
dataTable.Columns.Add(prop.Name, prop.PropertyType);
}
return dataTable;
}
-
June 1st, 2010, 01:57 PM
#8
Re: Convert Generic List To Dataset
Thx for adding Codetags. Great help for the poster, you will get my points
Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
June 1st, 2010, 04:20 PM
#9
Re: Convert Generic List To Dataset
Oooooh.... if this works hlaf as well as I think it will, I might be able to use this.
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|