locked
Filling a specific column of a datatable with data RRS feed

  • Question

  • how would I fill the second column of data in a datatable.

    I can fill the first by

    foreach (int somevalue in somelist)

    {

       mytable.rows.add(somevalue);

    }

    what about adding data to the second column later?


    chuckdawit

    Monday, June 4, 2012 11:07 PM

Answers

  • Simple,

    int i=0;
    foreach (int somevalue in somelist) { mytable.rows[i++][1] = somevalue; //Add value to second column of each row }


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


    Tuesday, June 5, 2012 5:21 AM
  • or:

    DataTable table = new DataTable();
    //create columns and fill it...
    for (int i = 0; i < table.Rows.Count; i++)
    {
         table.Rows[i][1] = "some value";
    }


    Mitja

    Tuesday, June 5, 2012 6:13 AM

All replies

  • You should notice it that your somelist should be something like Array,and then you can do that very easy!

    But please make sure that YOUR ARRAYLIST'S COUNT SHOULD BE EQUAL OR LESS THAN THAT OF COLUMNS OF A DATATABLE!

     DataTable dt = new DataTable();
                //Suppose two columns……
                dt.Columns.Add("id", typeof(int));
                dt.Columns.Add("name",typeof(string));
    
                //An array
                ArrayList al = new ArrayList();
                al.Add(1);
                al.Add("DDDD");
                dt.Rows.Add(al.ToArray());

    See MSDN of DataRowCollection' Add method——

    public DataRow Add(
    	params Object[] values
    )

       QQ我:讨论(Talk)
    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    Tuesday, June 5, 2012 2:42 AM
  • Simple,

    int i=0;
    foreach (int somevalue in somelist) { mytable.rows[i++][1] = somevalue; //Add value to second column of each row }


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


    Tuesday, June 5, 2012 5:21 AM
  • or:

    DataTable table = new DataTable();
    //create columns and fill it...
    for (int i = 0; i < table.Rows.Count; i++)
    {
         table.Rows[i][1] = "some value";
    }


    Mitja

    Tuesday, June 5, 2012 6:13 AM