Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

Suppose I have a datatable named dtRecord with SrNo, Name, Age and there are about 1000+ rows in the datatable.

Now I want to update the Age of selected name how can I do it? I am looking for a fast and accurate way.

Thanks in advance.
Kapil
Posted
Updated 1-Nov-18 20:45pm

 
Share this answer
 
There's two approaches I can think of...
C#
DataTable dt = new DataTable();

// Get all DataRows where the name is the name you want.
IEnumerable<datarow> rows = dt.Rows.Cast<DataRow>().Where(r => r["Name"].ToString() == "SomeName");
// Loop through the rows and change the name.
rows.ToList().ForEach(r => r.SetField("Name", "AnotherName"));

// Alternative approach.
// Simply loop through the rows, check the value of the Name field and change its value accordingly.
foreach (DataRow row in dt.Rows)
{
	if (row["Name"].ToString() == "SomeName")
		row.SetField("Name", "AnotherName");
}
Of course you should replace "SomeName" and "AnotherName" with your own variables.
Hope it helps!
 
Share this answer
 
v3
Comments
Md shabaz alam 2-Jul-18 5:24am    
it works. Thank you.
you can do like:
Table.AsEnumerable().Where(s=>Convert.ToString(s["Name"]).Equals("valuetocheck")).ToList().ForEach(D=>D.SetField("Age",AgevalueToUpdate));


This is the simplest way to update the specific column value in Table with condition
 
Share this answer
 

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