Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jan 10, 2013 12:56 AM by Decker Dong - MSFT
Member
94 Points
389 Posts
Jan 08, 2013 12:42 AM|LINK
i have 2 for loops in my code....
for the first for loop when m = 0....i'll get a new column called "COL" in my datatable...
now my problem is when m = 1.... i need another column called "feb" in my datatable
when my "m" changes my "da" also changes,,,so i get different calculation....
similarly for m=2...one more column similarly for m=3...one more column......till m = 6
ArrayList projects = new ArrayList(); DataColumn dc1 = new DataColumn("cal"); _table.Columns.Add(dc1);
for (int m = 0; m < _table1.Rows.Count; m++) {
int da = (Convert.ToInt16(_table1.Rows[m]["days"]));
for (int i = 0; i < _table.Rows.Count; i++) { int g;
{ g = (Convert.ToInt16(_table.Rows[i]["RE"]) * 100) / ((Convert.ToInt16(_table.Rows[i]["HC"]) * da)); }
projects.Add(g);
}
for (int k = 0; k < _table.Rows.Count; k++) { _table.Rows[k][dc1] = projects[k]; }
gvresmgt.DataSource = _table; gvresmgt.PageSize = Int16.Parse(ConfigurationManager.AppSettings["GridPageSize"]); gvresmgt.DataBind();
} }
Star
13599 Points
2691 Posts
Jan 08, 2013 05:25 AM|LINK
Hi,
You can try like the following if you have the column names in a list
private void button1_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); List<String> dtColumns = new List<string>(); dtColumns.Add("Column1"); dtColumns.Add("Column2"); dtColumns.Add("Column3"); dtColumns.Add("Column4"); dtColumns.Add("Column5"); dtColumns.Add("Column6");
for (int m = 0; m < 6; m++) { dt.Columns.Add(dtColumns[m].ToString()); } dt.AcceptChanges(); }
Try like the following,
39 Points
70 Posts
Jan 08, 2013 05:39 AM|LINK
try by writing new connection string
Dim str As String = Select '' As column, continue ur further string here.....
Eg
Dim str As String ="Select '' AS column, date_time FROM Calendar
this will automatically generates new column with name "column" in ut dt.
All-Star
118619 Points
18779 Posts
Jan 10, 2013 12:56 AM|LINK
anilr499 anilr499
I'm afraid you have to:
1) Use something like DataTable.Columns.Add(……) to dyanmically add columns.
2) Then keep your DataTable in memory (because Html is of non-memory), into a ViewState. If necessary, take it out for futher use.
anilr499
Member
94 Points
389 Posts
How to add a column to a datatble based on for loop value
Jan 08, 2013 12:42 AM|LINK
i have 2 for loops in my code....
for the first for loop when m = 0....i'll get a new column called "COL"
in my datatable...
now my problem is when m = 1.... i need another column called "feb" in my datatable
when my "m" changes my "da" also changes,,,so i get different calculation....
similarly for m=2...one more column
similarly for m=3...one more column......till m = 6
ArrayList projects = new ArrayList();
DataColumn dc1 = new DataColumn("cal");
_table.Columns.Add(dc1);
for (int m = 0; m < _table1.Rows.Count; m++)
{
int da = (Convert.ToInt16(_table1.Rows[m]["days"]));
for (int i = 0; i < _table.Rows.Count; i++)
{
int g;
{
g = (Convert.ToInt16(_table.Rows[i]["RE"]) * 100) / ((Convert.ToInt16(_table.Rows[i]["HC"]) * da));
}
projects.Add(g);
}
for (int k = 0; k < _table.Rows.Count; k++)
{
_table.Rows[k][dc1] = projects[k];
}
gvresmgt.DataSource = _table;
gvresmgt.PageSize = Int16.Parse(ConfigurationManager.AppSettings["GridPageSize"]);
gvresmgt.DataBind();
}
}
sarathi125
Star
13599 Points
2691 Posts
Re: How to add a column to a datatble based on for loop value
Jan 08, 2013 05:25 AM|LINK
Hi,
You can try like the following if you have the column names in a list
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
List<String> dtColumns = new List<string>();
dtColumns.Add("Column1");
dtColumns.Add("Column2");
dtColumns.Add("Column3");
dtColumns.Add("Column4");
dtColumns.Add("Column5");
dtColumns.Add("Column6");
for (int m = 0; m < 6; m++)
{
dt.Columns.Add(dtColumns[m].ToString());
}
dt.AcceptChanges();
}
Hi,
Try like the following,
Remember to click Mark as Answer on the post that helps to others.
My Blog :MyAspSnippets
DonIzzBack
Member
39 Points
70 Posts
Re: How to add a column to a datatble based on for loop value
Jan 08, 2013 05:39 AM|LINK
try by writing new connection string
Dim str As String = Select '' As column, continue ur further string here.....
Eg
Dim str As String ="Select '' AS column, date_time FROM Calendar
this will automatically generates new column with name "column" in ut dt.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How to add a column to a datatble based on for loop value
Jan 10, 2013 12:56 AM|LINK
Hi,
I'm afraid you have to:
1) Use something like DataTable.Columns.Add(……) to dyanmically add columns.
2) Then keep your DataTable in memory (because Html is of non-memory), into a ViewState. If necessary, take it out for futher use.