Answered by:
Filling a specific column of a datatable with data

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!
- Edited by Adavesh Tuesday, June 5, 2012 6:28 AM
- Proposed as answer by ThankfulHeart Tuesday, June 5, 2012 9:11 AM
- Marked as answer by Lisa ZhuModerator Thursday, June 7, 2012 5:39 AM
- Unmarked as answer by Lisa ZhuModerator Thursday, June 7, 2012 5:40 AM
- Marked as answer by Lisa ZhuModerator Monday, June 11, 2012 2:33 AM
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
- Proposed as answer by ThankfulHeart Tuesday, June 5, 2012 9:11 AM
- Marked as answer by Lisa ZhuModerator Thursday, June 7, 2012 5:39 AM
- Unmarked as answer by Lisa ZhuModerator Thursday, June 7, 2012 5:40 AM
- Marked as answer by Lisa ZhuModerator Monday, June 11, 2012 2:33 AM
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 )
- Edited by ThankfulHeart Tuesday, June 5, 2012 2:49 AM
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!
- Edited by Adavesh Tuesday, June 5, 2012 6:28 AM
- Proposed as answer by ThankfulHeart Tuesday, June 5, 2012 9:11 AM
- Marked as answer by Lisa ZhuModerator Thursday, June 7, 2012 5:39 AM
- Unmarked as answer by Lisa ZhuModerator Thursday, June 7, 2012 5:40 AM
- Marked as answer by Lisa ZhuModerator Monday, June 11, 2012 2:33 AM
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
- Proposed as answer by ThankfulHeart Tuesday, June 5, 2012 9:11 AM
- Marked as answer by Lisa ZhuModerator Thursday, June 7, 2012 5:39 AM
- Unmarked as answer by Lisa ZhuModerator Thursday, June 7, 2012 5:40 AM
- Marked as answer by Lisa ZhuModerator Monday, June 11, 2012 2:33 AM
Tuesday, June 5, 2012 6:13 AM