Rocky
Rocky

Reputation: 4524

How Convert DataSet to List<ArrayList>?

I have a dataset ds and a List of ArrayList newpath i want to add(assign) ds to newpath how is that possible.

public List<ArrayList> newpath
    {
        set { ViewState["newpath"] = value; }
        get
        {
            if (ViewState["newpath"] == null)
                return new List<ArrayList>();
            else
                return (List<ArrayList>)ViewState["newpath"];
        }
    }

i am trying with

foreach (DataRow dataRow in Ftb.Rows)//Ftb is datatable
 {
    newpath.Add(dataRow);//newpath is List<ArrayList>
 }

and

foreach (DataRow dRow in ds.Tables[0].Rows)
 {
   newpath.Add(dRow);
 } 

if i am doing like above way then i am getting the error "The best overloaded method match for 'System.Collections.Generic.List.Add(System.Collections.ArrayList)' has some invalid arguments"

Please help me if in someway.. how to do that

Upvotes: 0

Views: 13679

Answers (4)

Rocky
Rocky

Reputation: 4524

I have modified @Shadow Wizard code and now its working fine

List<ArrayList> newval = new List<ArrayList>();
    foreach (DataRow dRow in ds.Tables[0].Rows)
      {
        ArrayList values = new ArrayList();
        foreach (object value in dRow.ItemArray)
            values.Add(value);
            newval.Add(values);
      }
 newpath = newval;

Upvotes: 0

ipr101
ipr101

Reputation: 24236

From the code you've posted it looks as if your newpath variable is a list of ArrayLists. So when you try to add dRow which is DataRow you're getting an error as it's not an ArrayList.

If you want to add DataRows I think newpath should be a list of DataRow. If you wanted to populate the ArrayList with data from the DataRow you'd need something like this -

foreach (DataRow dataRow in Ftb.Rows)//Ftb is datatable
 {
      ArrayList myAL = new ArrayList();
      myAL.Add(dataRow["your_column_name"]);
      //add fields from DataRow to array list using your required logic
      newpath.Add(myAL);
 }

Upvotes: 0

Shadow Wizard
Shadow Wizard

Reputation: 66388

I assume you want each item in newpath to contain the column values of each row?
If so, have such code instead:

foreach (DataRow dataRow in Ftb.Rows)//Ftb is datatable
{
    ArrayList values = new ArrayList();
    foreach (object value in dataRow.ItemArray)
        values.Add(value);
    newpath.Add(values);
}

Upvotes: 3

juergen d
juergen d

Reputation: 204844

change newpath from

List<ArrayList> 

to

List<DataRow>

Upvotes: 0

Related Questions