Click here to Skip to main content
12,999,744 members (40,421 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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.
Posted 7-Apr-12 9:21am
Updated 27-May-16 3:03am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

There's two approaches I can think of...
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!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 10 Jun 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100