如何更新数据集 [英] How to update a dataset
问题描述
在我的项目中,有两个文本框, txtName
和 txtPopulation
和一个按钮, btnClick
。每当用户单击 btnClick
时,数据集 dsDetails
的人口列中的值应按该值更新在 txtPopulation
中,其中名称列等于 txtName
。我知道可以使用 rowfilter
或 select
完成此操作,但我不知道该在其中实现什么。 请不要使用
In my project, there are two textBoxes, txtName
and txtPopulation
and a Button, btnClick
. whenever the user clicks btnClick
, the value in the "Population" column of the dataset dsDetails
should get updated by the value in txtPopulation
, where the "Name" Column is equal to txtName
. I know this can be done using rowfilter
or select
but I have no idea what to implement in it. Please no linq
当前,我正在做这样的事情。.( working )
for (int intCount = 0; intCount < dsDetails.Tables[0].Rows.Count; intCount++)
{
if (lblCountryName.Text.Equals(dsDetails.Tables[0].Rows[intCount][0].ToString()))
{
dsDetails.Tables[0].Rows[intCount][3] = txtPopulation.Text;
}
}
推荐答案
您需要在数据表上调用 .AcceptChanges()
,以便将更改提交给集合,例如:
You need to call .AcceptChanges()
on your DataTable so your changes are committed to the collection, like this:
for (int intCount = 0; intCount < dsDetails.Tables[0].Rows.Count; intCount++)
{
if (lblCountryName.Text.Equals(dsDetails.Tables[0].Rows[intCount][0].ToString()))
{
dsDetails.Tables[0].Rows[intCount][3] = txtPopulation.Text;
}
}
dsDetails.Tables[0].AcceptChanges();
使用选择行过滤器
您可以使用定位列。选择
行过滤器,如下所示:
You can target your column by using the .Select
row filter, like this:
foreach (DataRow row in dsDetails.Tables[0].Select("Name = '" + txtName.Text + "'"))
{
row[3] = txtPopulation.Text;
}
dsDetails.Tables[0].AcceptChanges();
这篇关于如何更新数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!