Click here to Skip to main content
15,905,915 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
on adding new column to DataTable my previous column data are deleted
Posted

try this sample..


C#
DataTable dt = new DataTable();
               dt.Columns.Add("amount",typeof(double));

               dt.Rows.Add(123456789);
               dt.Rows.Add(3434);
               dt.Rows.Add(656565665);

               dt.Columns.Add("newcolumn", typeof(string));
 
Share this answer
 
use below:
DataTable dt = new DataTable();
DataRow row = dt.NewRow();
row[0] = "value";
row[1] = "value";
dt.Rows.Add(row);
 
Share this answer
 
Comments
OriginalGriff 26-Nov-13 6:34am    
Reason for my vote of one: Please read the question carefully before answering.
"on adding new **column** to DataTable my previous **column** data are deleted"
Your code has nothing whatsoever to do with columns, it adds a row...
At a guess, don't use MyDataTable.Columns.Clear() before you add a new column...
 
Share this answer
 
Comments
satyaanupama 26-Nov-13 6:15am    
i think its correct........

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900