Domanda

i am trying to show more then one value in datarow i tried it with different ways but getting error my code is,

 crow["BaseCostHighWay * POF * PTF * WCF"] = BaseCostScoreHW + POF + PTF + WCF;

i trying to show "BaseCostScoreHW " "POF" "PTF" "WCF" in my data row if i try it with '"+"' between them result in error on this place

Hopes for your suggestion thanks in advance

EDITED,

should be look like

BaseCostHighWay * POF * PTF * WCF

2.5,1.6,8.1,0.9
È stato utile?

Soluzione

Depending on the types of variables 'BaseCost..', 'PDF', 'PTF' etc you may get various errors. So, for starters, when asking, always say WHAT error you are getting or else we'd have to take a crystall ball and guess.

Another thing is, what do you mean by "+"? Do you want to add up the numbers, or do you want to glue the text together?

Guessing from typical problems, the most probable is that you want to build a string with multiple 'values' inside and your variables are of mixed type. Try adding ".ToString()" after each other and check if the error occurs again.

string text = BaseCostScoreHW.ToString() + POF.ToString() + PTF.ToString() + WCF.ToString();
crow["BaseCostHighWay"] = text;
// text = 1.12.23.34.4

That's a bit verbose. If you really want to use '+' operator, just make sure that first of the value is a string:

string text1 = BaseCostScoreHW.ToString() + POF + PTF + WCF;
string text2 = string.Empty + BaseCostScoreHW + POF + PTF + WCF;
string text3 = "" + BaseCostScoreHW + POF + PTF + WCF;
// text1/2/3 = 1.12.23.34.4

Those three lines have identical effect.

But, even if it works, the result will not be pretty, as it will glue up everything tightly. You may add more strings and characters to the expressions:

string text = "" + BaseCostScoreHW + "," + POF + "," + ...

But .. thats hardly mantainable. A nicer way to format a bit of text is to use .. string.format:

string text = string.Format("{0},{1},{2},{3}", BaseCostScoreHW, POF, PTF, WCF);
crow["BaseCostHighWay"] = text;
// text = 1.1,2.2,3.3,4.4

Note that there's no ambiguous "+" here. You just say "format" and "like this template" and "here are values".

Altri suggerimenti

Have you tried DataColumn? DataColumn Class

crow["BaseCostHighWay * POF * PTF * WCF"] = 
    String.Format("{0},{1},{2},{3}", BaseCostScoreHW, POF, PTF, WCF);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top