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();
Re: Convert Generic List To Dataset
Quote:
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 :D
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..
Re: Convert Generic List To Dataset
Quote:
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 :wave:
Here is one for creation of a DataTable
http://dotnetperls.com/datatable-use
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.
Re: Convert Generic List To Dataset
Quote:
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 ? :D
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;
}
Re: Convert Generic List To Dataset
Thx for adding Codetags. Great help for the poster, you will get my points:D
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