Link to home
Start Free TrialLog in
Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on

How do I update a DataColumn value in C#?

How do I update a DataColumn's value in C#?


I have the DataRow object.


And this works, BUT I need to push the changes to the DataTable.


                                productShortfallRow.BeginEdit();

                                productShortfallRow["SHORTFALL_RESOLVED"] = "YES";

                                productShortfallRow.EndEdit();


Do I read the row, Delete the row, the Add the row?


I am inside the iterator on the DataTable. Are updates allowed there? I can see Add and Delete are not.


Thanks

Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

The DataRow is attached to the DataTable, so as soon as you update a value on the row, it is reflected in the DataTable. You can add/remove rows simply by working with the "Rows" collection property on the DataTable. That Rows object / collection has the methods to add/remove rows. 

Avatar of curiouswebster

ASKER

I would rather Update, is possible.

I now do this, but it still fails to persist the changes:

                                productShortfallRow.BeginEdit();
                                productShortfallRow["SHORTFALL_RESOLVED"] = "YES";
                                productShortfallRow.EndEdit();
                                productShortfallRow.AcceptChanges();

Then, when done with all rows, I call the list, thusly:
            productShortfalls.AcceptChanges();

It's unclear what you mean when you say it "fails to persist the changes" - can you explain what you are seeing?

I review the DataTable and  find the  default value of "NO" is contained inn every row, even though most have been set to "YES". It's somehow disconnected from the original DataTable.

Could it be  that I have passed the  DataTable into the function as an input parameter? Then, updating a row's value inside an iterator?
So, I could create a parallel DataTable, then add the updated row. Is there a quick way to clone the row's values into a new DataRow?

How do you review the DataTable?

I iterate through, row by row. Is that what you mean?

I just need to get than updated value to "stick". My hack of a parallel list it terrible. Already, it is crashing since I need to Clone each row, then update.

Suggestions?
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My  code is quite large, and I THOUGHT I was doing what you suggested. It is fixed now. YOur comments helped a lot. Thanks.