Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all

C#
C# Problem


i have a data table with 5 columns

column 4 is Georgian Date

how do i change
C#
all content of this column (Column 4)
and save it again (save in column 4 again == each data after changed placed in old place )

for example :

i have this data table :

C#
a   b   c   d   e   f
a   b   c   d   e   f
a   b   c   d   e   f
a   b   c   d   e   f


after change e to z data table like below :

C#
a   b   c   d   z   f
a   b   c   d   z   f
a   b   c   d   z   f
a   b   c   d   z   f



how do i ?

Please Help Me

Thanks A Lot
Posted
Comments
VJ Reddy 21-May-12 3:00am    
Thank you for accepting the solution :)

You can for example loop through the data rows and modify the values of each row. Something like this (code not checked with the compiler)
C#
foreach (System.Data.DataRow row in mydatatable.Rows) {
   row[4] = "Z";
}
mydatatable.AcceptChanges();

Afterwards, you can save the datatable somewhere (to a file, database etc).
 
Share this answer
 
Comments
VJ Reddy 20-May-12 1:35am    
Good answer 5!
Wendelius 20-May-12 3:07am    
Thanks :)
The answer 1 is good.
As an alternative if you want to use LINQ then the following code can be used

C#
data.AsEnumerable().Where(dr => dr.Field<string>(4)=="e")
        .Select(dr => dr.SetField<string>(4,"z")).Count();

Where data is the DataTable
The LINQ has deferred execution, so it will not execute the query until the sequence returned is iterated or a scalar function like Count is called. Hence, Count is called in the above code to immediately execute the query.
 
Share this answer
 
v3
Comments
Wendelius 20-May-12 3:08am    
Also a good solution!
VJ Reddy 20-May-12 9:23am    
Thank you, Mika :)

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