How can I cast the function of IList generic collections into DataTable, as I want to generate data from mySQL database to set them into placeholder????
if we assume that you have the following definition for SearchInfo:
public class SearchInfo
{
private string field1;
private int field2;
private double field3;
}
and you have the function :
public IList<SearchInfo> GetSearchList()
{
// do sth here that retrieves the info as Ilist
}
you could have the following function that converts IList into DataTable (no straight cast is available for such conversion):
public DataTable GetSearch()
{
IList<SearchInfo> list = GetSearchList();
DataTable t = new DataTable();
t.Columns.Add("Field1", typeof(string));
t.Columns.Add("Field2", typeof(int));
t.Columns.Add("Field3", typeof(double));
foreach(SearchInfo s in list)
{
DataRow row = t.NewRow();
row["Field1"]=s.field1;
row["Field2"]=s.field2;
row["Field3"]=s.field3;
if we assume that you have the following definition for SearchInfo:
public class SearchInfo
{
private string field1;
private int field2;
private double field3;
}
and you have the function :
public IList<SearchInfo> GetSearchList()
{
// do sth here that retrieves the info as Ilist
}
you could have the following function that converts IList into DataTable (no straight cast is available for such conversion):
public DataTable GetSearch()
{
IList<SearchInfo> list = GetSearchList();
DataTable t = new DataTable();
t.Columns.Add("Field1", typeof(string));
t.Columns.Add("Field2", typeof(int));
t.Columns.Add("Field3", typeof(double));
foreach(SearchInfo s in list)
{
DataRow row = t.NewRow();
row["Field1"]=s.field1;
row["Field2"]=s.field2;
row["Field3"]=s.field3;
Thanks for ur post.It works fine but the execution time is faster for larger number of datas.Whether you tell me a another solution for adding datas to datatable without looping.
Ranine2007
Member
26 Points
54 Posts
Casting the IList functions into DataTable
Jan 21, 2007 06:10 AM|LINK
Hi,
How can I cast the function of IList generic collections into DataTable, as I want to generate data from mySQL database to set them into placeholder????
//search//
public IList<SearchInfo> GetSearch(){
}
//====> to cast it this way//
public DataTable GetSearch(){
}
I am in need for a help [:S]!
Regards,
Ranine
Yani Dzhurov
Contributor
2646 Points
516 Posts
Re: Casting the IList functions into DataTable
Jan 22, 2007 03:32 PM|LINK
Hi,
if we assume that you have the following definition for SearchInfo:
public class SearchInfo
{
private string field1;
private int field2;
private double field3;
}
and you have the function :
public IList<SearchInfo> GetSearchList()
{
// do sth here that retrieves the info as Ilist
}
you could have the following function that converts IList into DataTable (no straight cast is available for such conversion):
public DataTable GetSearch()
{
IList<SearchInfo> list = GetSearchList();
DataTable t = new DataTable();
t.Columns.Add("Field1", typeof(string));
t.Columns.Add("Field2", typeof(int));
t.Columns.Add("Field3", typeof(double));
foreach(SearchInfo s in list)
{
DataRow row = t.NewRow();
row["Field1"]=s.field1;
row["Field2"]=s.field2;
row["Field3"]=s.field3;
t.Rows.Add(row);
}
return t;
}
However, this is almost meta code.
You need to adjust it for your needs.
Cheers,
Yani
Yani Dzhurov
Contributor
2646 Points
516 Posts
Re: Casting the IList<---> functions into DataTable
Jan 22, 2007 03:32 PM|LINK
Hi,
if we assume that you have the following definition for SearchInfo:
public class SearchInfo
{
private string field1;
private int field2;
private double field3;
}
and you have the function :
public IList<SearchInfo> GetSearchList()
{
// do sth here that retrieves the info as Ilist
}
you could have the following function that converts IList into DataTable (no straight cast is available for such conversion):
public DataTable GetSearch()
{
IList<SearchInfo> list = GetSearchList();
DataTable t = new DataTable();
t.Columns.Add("Field1", typeof(string));
t.Columns.Add("Field2", typeof(int));
t.Columns.Add("Field3", typeof(double));
foreach(SearchInfo s in list)
{
DataRow row = t.NewRow();
row["Field1"]=s.field1;
row["Field2"]=s.field2;
row["Field3"]=s.field3;
t.Rows.Add(row);
}
return t;
}
However, this is almost meta code.
You need to adjust it for your needs.
Cheers,
Yani
Ranine2007
Member
26 Points
54 Posts
Re: Casting the IList functions into DataTable
Jan 25, 2007 09:05 AM|LINK
shanker
Member
237 Points
106 Posts
Re: Casting the IList<---> functions into DataTable
Jan 27, 2010 04:05 PM|LINK
Dear Yani,
Thanks for ur post.It works fine but the execution time is faster for larger number of datas.Whether you tell me a another solution for adding datas to datatable without looping.
Thanks in advance.
<div style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;" id="_mcePaste">LMW_Tbl_Engineer</div>