Click here to Skip to main content
15,312,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi have datatable like,

VB
Column1 | Column2
1        NULL
2        NULL
3        NULL
4        NULL


I Want datatable Like

VB
Column1 | Column2
1         1
2         2
3         3
4         4


for this what i have to do in .cs file.
Posted
Updated 27-Jul-20 6:51am
v2
Comments
[no name] 6-Jun-14 9:34am
   
"write code of .cs file", no. That is not how this works. We do not write code to order. You need to give this a try first and then ask a question if you run into an actual problem.
pareshpbhayani 6-Jun-14 9:41am
   
I apologize for that, i have tried myself but i could not get any shortcut solution.

Something like this should get you started:

C#
for(int i = 0; i < dataTable.Rows.Count; i ++)
        dataTable[i]["Column2"]=dataTable[i]["Column1"].ToString();


Hope it helps.
   
Comments
pareshpbhayani 6-Jun-14 9:38am
   
but is there any shortcut for it?
ZurdoDev 6-Jun-14 9:47am
   
That IS the shortcut. It is extremely simple to do. What were you hoping for?
pareshpbhayani 6-Jun-14 10:08am
   
I have lost of rows in my datatable so loop take 10 to 15 sec to copy data from one column to another column.
hypermellow 6-Jun-14 10:12am
   
How are you populating your DataTable? - could you copy the column values as you populate?
pareshpbhayani 6-Jun-14 10:45am
   
I am getting datatable from database.
Nayan Rudani 7-Jun-14 2:43am
   
if you get DataTable From Database then get Column2 as Alis of Column1
foreach(DataRow dr in dataTable.Rows)
{
dr["NewColumn"] = dr[OldColumn];
}
   

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