在DataTable中查找和更新单元格 [英] Find and Update cell in DataTable
本文介绍了在DataTable中查找和更新单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
假设我有一个名为dtRecord的数据表,其中包含SrNo,Name,Age,数据表中有大约1000多行。
现在我想更新所选名称的年龄我该怎么办?我正在寻找一种快速准确的方法。
提前致谢。
Kapil
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
推荐答案
看一下这篇文章:
http://msdn.microsoft.com/en-us/library/tat996zc%28v=vs.100%29.aspx [ ^ ]
干杯
Hi,
Take a look at this post:
http://msdn.microsoft.com/en-us/library/tat996zc%28v=vs.100%29.aspx[^]
Cheers
我能想到的两种方法......
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");
}
当然你应该替换SomeName
和AnotherName
用你自己的变量。
希望它有所帮助!
Of course you should replace "SomeName"
and "AnotherName"
with your own variables.
Hope it helps!
你可以这样做:
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
这篇关于在DataTable中查找和更新单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文