We help IT Professionals succeed at work.

I need to iterate through a DataTable and change data conditionally

tatton777
tatton777 asked
on
Medium Priority
239 Views
Last Modified: 2010-04-16
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?

Comment
Watch Question

Commented:
DataTable dataTable;
// instantiate and load dataTable
foreach (DataRow r in dataTable.Rows) {
  if (r["FeeGroup"] == "R") {
    r["FeeGroup"] = "Required";
  }
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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!!

Commented:
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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.