Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update a dataset

Tags:

c#

dataset

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

enter image description here Currently, 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;
    }
}
like image 807
Mr_Green Avatar asked Nov 18 '25 07:11

Mr_Green


1 Answers

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

Using select row filter

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();
like image 155
Paul Aldred-Bann Avatar answered Nov 20 '25 21:11

Paul Aldred-Bann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!