user1237226
user1237226

Reputation:

C# Insert ArrayList in DataRow

I want to insert an arraylist in Datarow.

using this code,

      ArrayList array=new ArrayList();
      foreach (string s in array) 
      {
           valuesdata.Rows.Add(s);
      }

But My datatable must have only one datarow. My code created eight datarows. I tried,

valuesdata.Rows.Add(array);

But it doesn't work.That should be

valuesdata.Rows.Add(array[0],array[1],array[2],array[3]....);

How can I solve this problem?

Thanks.

Upvotes: 1

Views: 6264

Answers (2)

John Woo
John Woo

Reputation: 263843

try this:

        ArrayList array = new ArrayList();

        String[] arrayB = new String[array.Count];
        for (int i = 0; i < array.Count; i++)
        {
            arrayB[i] = array[i].ToString();
        }

        valuesdata.Rows.Add(arrayB);

Upvotes: 4

Davide Piras
Davide Piras

Reputation: 44605

try like this:

//1 - declare the array ArrayList object
ArrayList array = new ArrayList();

//2 - here add some elements into your array object

//3 - convert the ArrayList to string array and pass it as ItemArray to Rows.Add
valuesdata.Rows.Add(array.ToArray(typeof(string)));

Upvotes: 3

Related Questions