I need to iterate through a DataTable and change data conditionally

I have a instantiated, loaded, DataTable with the following columns:

FeeID
FeeGroup
Comment
Price

I need to itereate through the table and anytime that FeeGroup = "R" I want that field changed to "Required".

Can anyone help?

LVL 1
tatton777Asked:
Who is Participating?
 
wlfsCommented:
DataTable dataTable;
// instantiate and load dataTable
foreach (DataRow r in dataTable.Rows) {
  if (r["FeeGroup"] == "R") {
    r["FeeGroup"] = "Required";
  }
}
0
 
tatton777Author Commented:
Thanks for the help. Just wanted to comment that to get the compiler to throw no warnings, I need to write the code like this:

    foreach (DataRow r in dataTable.Rows)
    {
      if (r["FeeGroup"].ToString() == "R")
      {
        r["FeeGroup"] = "Required";
      }
    }

Just a note to anyone else who might be trying this. The ReadOnly property on the TableColumn must be set to "false".

THANKS WLFS!!
0
 
wlfsCommented:
Yes, sorry that I forgot about the ToString().

> The ReadOnly property on the TableColumn must be set to "false".
Well, that should be fairly obvious :)

Glad I could help,
woolf
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.