C# 如何更新数据集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13008748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:19:38  来源:igfitidea点击:

How to update a dataset

c#dataset

提问by Mr_Green

In my project, there are two textBoxes, txtNameand txtPopulationand a Button, btnClick. whenever the user clicks btnClick, the value in the "Population" column of the dataset dsDetailsshould get updated by the value in txtPopulation, where the "Name" Column is equal to txtName. I know this can be done using rowfilteror selectbut I have no idea what to implement in it. Please no linq

在我的项目中,有两个文本框,txtNametxtPopulation和一个按钮,btnClick。每当用户单击 时btnClick,数据集“人口”列中dsDetails的值应由 中的值更新txtPopulation,其中“名称”列等于txtName。我知道这可以使用rowfilteror来完成,select但我不知道要在其中实现什么。请不要linq

enter image description hereCurrently, I am doing something like this..(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;
    }
}

采纳答案by Paul Aldred-Bann

You need to call .AcceptChanges()on your DataTable so your changes are committed to the collection, like this:

您需要调用.AcceptChanges()DataTable 以便将更改提交到集合,如下所示:

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();

Using select row filter

使用选择行过滤器

You can target your column by using the .Selectrow filter, like this:

您可以使用.Select行过滤器来定位您的列,如下所示:

foreach (DataRow row in dsDetails.Tables[0].Select("Name = '" + txtName.Text + "'"))
{
    row[3] = txtPopulation.Text;
}

dsDetails.Tables[0].AcceptChanges();

回答by andy

DataRow[] dr = dsDetails.Tables[0].Select("Something='"+lblCountryName.Text+"'");
if(dr.Length > 0)
{
 dr[0][0] = "ChangeValue";//Datarow is reference to datatable it will automatically update the datatable values
}
dsDetails.Tables[0].AcceptChanges();