Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data table cell math

Tags:

c#

datatable

I am very new to the whole thing of C# and would tremendously appreciate your wisdom on this: I am making a game-like thing that needs to store and constantly update a database for a population of people.

Currently I am trying to create a DataTable dt;

Add columns dt.Colums.Add("age", typeof(int));

Add rows dt.Rows.Add(10);

Now I would like to dynamically adjust the content of the cell which holds the age int value of 10 to 11.

I try to dt.Rows[0]["age"] += 1;

However, I get the following:

Error CS0266 Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?)

I am rather confused on why the "cell" returns an object rather than an int. How would it be possible to do math operations directly with the contents of the cell?

like image 515
VIENNADZIS Avatar asked Aug 14 '26 23:08

VIENNADZIS


1 Answers

Here dt.Rows[0]["age"] is an object and not Integer type so Operator '+=' cannot be applied to operands of type 'object' and 'int' Try this,

        dt.Rows[0]["age"] = Convert.ToInt16(dt.Rows[0]["age"]) + 1;
like image 200
Avdhoota Avatar answered Aug 16 '26 12:08

Avdhoota



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!