none
Dynamically adding columns to datatabe and adding value RRS feed

  • Question

  • Hi List,

    I am trying to dynamically add new column to a datatable, then add value for the added column:

    lets say I create a datatable and then add column "test":

    Datattable dt = new datatable();

    dt.columns.add(“test“,typeof(int))

    dt.rows.add(2)

    dt.rows.add(3)

    dt.rows.add(4)

    Now can I simply add another column "mycolumn" and then add value:

    dt.columns.add(“mycolumn“,typeof(int))

    Can I say: dt.rows[0]["mycolumn"]=3

    dt.rows[1]["mycolumn"]=7

    dt.rows[2]["mycolumn"]=6

    If not, would you please let me know how I can do this?

    thanks

    Arvin

    Thursday, November 7, 2013 2:18 PM

Answers

  • thanks, no I have not done it yet, just wanted to double check. the first approach, I did not add any row, then it failed. Then I decided to do this, but wanted to check. So if we add 5 rows, next time I add a column, that column also has 5 rows, correct?

    Yes. That's correct.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by azolfaghari Thursday, November 7, 2013 4:18 PM
    Thursday, November 7, 2013 3:19 PM
  • Where is the data coming from? I maybe able to help you load the data into the data table very easily using Linq.  If you are doing a query from a database the you can fill the data in one instruction using a dataadapter.  if yoiu give us a brief description of your project we can probably simplify the code a lot.


    jdweng

    • Marked as answer by azolfaghari Friday, November 8, 2013 9:06 AM
    Thursday, November 7, 2013 3:58 PM

All replies

  • Of course. That's the way you do it. Are you facing any issue with that approach? Or you just wanted to know whether it's the right approach?


    Please mark this post as answer if it solved your problem. Happy Programming!

    Thursday, November 7, 2013 2:34 PM
  • thanks, no I have not done it yet, just wanted to double check. the first approach, I did not add any row, then it failed. Then I decided to do this, but wanted to check. So if we add 5 rows, next time I add a column, that column also has 5 rows, correct?
    Thursday, November 7, 2013 2:55 PM
  • Here is another method of initiaizing data in a DataTable

           static void Main(string[] args)
            {
                List<List<object>> data = new List<List<object>>() {
                    new List<object>(){"abc",1,10},
                    new List<object>(){"def",2,20},
                    new List<object>(){"abc",3,30}
                };
                DataTable dt = new DataTable();
                dt.Columns.Add("ID",typeof(string));
                dt.Columns.Add("test",typeof(int));
                dt.Columns.Add("mycolumn",typeof(int));
                foreach(List<object> row in data)
                {
                    DataRow newRow = dt.Rows.Add();
                    newRow.ItemArray = row.ToArray();
                }


    jdweng

    Thursday, November 7, 2013 3:00 PM
  • thanks, no I have not done it yet, just wanted to double check. the first approach, I did not add any row, then it failed. Then I decided to do this, but wanted to check. So if we add 5 rows, next time I add a column, that column also has 5 rows, correct?

    Yes. That's correct.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by azolfaghari Thursday, November 7, 2013 4:18 PM
    Thursday, November 7, 2013 3:19 PM
  • thanks, but the point here is that this is dynamic, and I do not know how many column I will have.
    Thursday, November 7, 2013 3:36 PM
  • Where is the data coming from? I maybe able to help you load the data into the data table very easily using Linq.  If you are doing a query from a database the you can fill the data in one instruction using a dataadapter.  if yoiu give us a brief description of your project we can probably simplify the code a lot.


    jdweng

    • Marked as answer by azolfaghari Friday, November 8, 2013 9:06 AM
    Thursday, November 7, 2013 3:58 PM
  • thanks, it reads data from other source, then for the number of cases, it adds a column to datatable. I am happy to go with my approach if that works.
    Thursday, November 7, 2013 4:28 PM