Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# DataTable find update
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 7-Apr-12 10:21am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
good
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!
  Permalink  
v3

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

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 7 Apr 2012
Copyright © CodeProject, 1999-2014
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