Question

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)) + " %";

    }
Était-ce utile?

La solution

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);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top