yash fale yash fale - 1 year ago 95
C# Question

how to get dataTable from arraylist in which dataTable is stored?

I have an

ArrayList
, it has a
string
at 0th position and
DataTable
at 1st position
is it possible to get
DataTable
back?

ArrayList dataModel = new ArrayList();
DataTable dt = new DataTable();
dt.Columns.Add("A");
dt.Columns.Add("B");
dt.Columns.Add("C");
dt.Rows.Add("1","Rahul","Vapi");
dt.Rows.Add("2", "Yash", "Vapi");
dt.Rows.Add("3", "Dinesh", "vapi");

dataModel.Add("this is working for me");
dataModel.Add(dt);


I am getting this error :


Cannot implicitly convert type 'object' to 'System.Data.DataTable'. An explicit conversion exists (are you missing a cast?)

Answer Source

(are you missing a cast?)

Yes.

DataTable dt = (DataTable) dataModel[1];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download