locked
Var Row into List RRS feed

  • Question

  • Hi,

    i am doing this var row = dt.Rows.Add(InitialAudit.Tables[0].Rows[i].ItemArray);

    Now i want to save row it in a data structure.IT can be a list?

    Can someone help please.

    Friday, September 7, 2012 10:36 AM

Answers

  • Hi,

    you can do a loop ove the ItemArray`s Length property of the Row object:

                var row = dt.Rows.Add(...);
                List<object> list = new List<object>();
                for (int i = 0; i < a.ItemArray.Length; i++)
                {
                    list.Add(a.ItemArray[i]);
                }


    Mitja

    • Proposed as answer by Jason Dot Wang Tuesday, September 11, 2012 4:42 AM
    • Marked as answer by Jason Dot Wang Monday, September 17, 2012 8:04 AM
    Friday, September 7, 2012 11:45 AM

All replies

  • You mean into List<T>? And column names from datatabe should be the properties inside the list?

    Mitja

    Friday, September 7, 2012 10:42 AM
  • Can you elaborate more on this.

    With Warm Regards

    Amit

    Friday, September 7, 2012 10:45 AM
  • Thanks for looking into it:)

    I have declared a var row variable and  i am inserting a data table row in it. Now i want to save the row in to some data structure. Data structure can be of any kind it can be list.

    So the question is to how i am going to save the Row into List or some other data structure. 

    Friday, September 7, 2012 10:59 AM
  • Hi,

    you can do a loop ove the ItemArray`s Length property of the Row object:

                var row = dt.Rows.Add(...);
                List<object> list = new List<object>();
                for (int i = 0; i < a.ItemArray.Length; i++)
                {
                    list.Add(a.ItemArray[i]);
                }


    Mitja

    • Proposed as answer by Jason Dot Wang Tuesday, September 11, 2012 4:42 AM
    • Marked as answer by Jason Dot Wang Monday, September 17, 2012 8:04 AM
    Friday, September 7, 2012 11:45 AM
  • In first place, I believe "dt" in your following statement is a DataTable. If so, it's Rows.Add method returns "void", so how can you assign it to a vaiable ?

    var row = dt.Rows.Add(InitialAudit.Tables[0].Rows[i].ItemArray);

    Also, why do you want to store a row in some data structure? IMO, the best data structure to store a row is the "DataTtable", which I believe you already have. So you need to be explicit in what exactly you are looking for?


    If this answers your question, please Mark it as Answer. If this post is helpful, please vote as helpful.

    Friday, September 7, 2012 1:24 PM
  • In first place, I believe "dt" in your following statement is a DataTable. If so, it's Rows.Add method returns "void", so how can you assign it to a vaiable ? 

    It does return mate.

    var is actually a DataRow object:

                DataTable table = new DataTable();
                table.Columns.Add("a1", typeof(int));
                table.Rows.Add(1);
                var q1 = table.Rows.Add(2);
                DataRow d2 = table.Rows.Add(3);


    ---

    Also, why do you want to store a row in some data structure?

    As I assume, he want to get the data of all Datacolumn array (of all columns) from one datarow into a list<T>. Thats why.


    Mitja

    Friday, September 7, 2012 1:30 PM