Slasher_bd
Slasher_bd

Reputation: 61

how to dynamically get a single value from a datatable in C#?

I am new at C#, I have a DataTable named dt; Now I want to get the values from it's each row and a specific column named "Number" from which I can calculate a third column to add. But cant' do it. Any ideas? Please help me.

foreach (DataRow dRow in dt.Rows)
    {
        int number = dt.Rows[0].Field<int>(1);

        dRow[Ratio] = Convert.ToString(((number * 100) / grandTotal)) + " %";

    }

Upvotes: 0

Views: 2078

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460138

Use a loop and the Field method, presuming that Number is an int:

foreach(DataRow row in dt.Rows)
{
    int number = row.Field<int>("Number");
    // do your calculation
    row.SetField("ThirdColumn", someValue);
}

Upvotes: 2

Related Questions