Kevin
Kevin

Reputation: 4848

How can I check, then change, the value in a datatable cell?

I have a datatable, transformersDT, populated with data from a database table. I'd like to check the value in a particular cell (row 0, column 6) and change that value based on what I find. How can I accomplish this in C#?

For instance, if the value is "0002" then I'd like to change it to "Conventional". Basically, I'm trying to make the values more "human readable" when viewed on the screen.

I'm trying to do something like this:

            if (transformerDT.Rows[0][6] == "0002")
            {
                transformerDT.Rows[0][6] = "Conventional";
            }

Upvotes: 1

Views: 193

Answers (3)

Jay Riggs
Jay Riggs

Reputation: 53595

You're close:

if (transformerDT.Rows[0][6].ToString() == "0002") {
    transformerDT.Rows[0][6] = "Conventional";
} 

You correctly referenced the row and column but you needed to cast the cell's content to a string before you run your comparison.

Upvotes: 1

Yahia
Yahia

Reputation: 70369

you could use transformersDT.Rows[0][5] for example.

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94625

You can use Rows collection property of DataTable.

object value=transformersDT.Rows[0][0]; //1st row & 1st column

Upvotes: 0

Related Questions