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 v开发者_运维问答alue 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";
}
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.
You can use Rows
collection property of DataTable
.
object value=transformersDT.Rows[0][0]; //1st row & 1st column
you could use transformersDT.Rows[0][5]
for example.
精彩评论