Click here to Skip to main content
11,932,103 members (58,681 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# DataTable

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 10:21am
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
Web03 | 2.8.151126.1 | Last Updated 7 Apr 2012
Copyright © CodeProject, 1999-2015
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