-1

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
tester Joe
  • 73
  • 2
  • 5
  • 15
  • 1
    Shouldn't `BaseCostHighWay`, `POF`, `PFT` and `WCF` all be individual columns? In other words, `crow["BaseCostHighWay"] = BaseCostScoreHW; crow["POF"] = POF; crow["PTF"] = PTF; crow["WCF"] = WCF;" or something similiar. – Tim Jun 06 '13 at 07:42
  • You obviously can't do like that. Can you provide the more code (what is crow, BaseCostScoreHW, POF, PTF and WCF, their types and so on)? – Alex Jun 06 '13 at 07:42
  • Is it a row in a DataTable you are trying to add? Or modify? – SamiHuutoniemi Jun 06 '13 at 07:44
  • @Tim it these are not rows that are variables having value – tester Joe Jun 06 '13 at 07:44
  • @voo BaseCostScoreHW, POF, PTF and WCF these are of type double variables having values like 2.3 ,.. and so on – tester Joe Jun 06 '13 at 07:45
  • @testerJoe - Ok...so what are the columns in your `DataRow`? Or are you trying to add all the values and put them into one column? – Tim Jun 06 '13 at 07:45
  • @Sami i am trying to show these variable (BaseCostScoreHW, POF, PTF and WCF) values in one row crow["BaseCostHighWay"] – tester Joe Jun 06 '13 at 07:47
  • Yes, but what kind of row? Is it the System.Data.DataRow, that goes into a System.Data.DataTable? – SamiHuutoniemi Jun 06 '13 at 07:49
  • @testerJoe - `crow["BaseCostHighWay"]` is the column named "BaseCostHigWay" in your DataRow `crow`. You can't reference multiple columns the way you appear to be trying to. You have to reference them individually. – Tim Jun 06 '13 at 07:49

3 Answers3

1

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".

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
  • I've updated the formats and values according to your example. Please review. – quetzalcoatl Jun 06 '13 at 07:52
  • Ah, and of course your DataColumn named "BaseCostHighWay * POF * PTF * WCF" must be of a string type. If you type it as a 'number'/'double'/etc it will not accept the constructed text value. Ensure that the column 'BaseCostHighWay * POF * PTF * WCF' has string-like datatype. – quetzalcoatl Jun 06 '13 at 07:54
  • is there any alternate because i have column of datatyp double :( – tester Joe Jun 06 '13 at 08:13
  • You have many options. The most basic one is to simply change the type of that column to String (in the query/application! not in the real table in the database!). Or, you can manually add a new 'string' column to your datatable and use it instead of that 'double' one (see http://msdn.microsoft.com/en-us/library/hfx3s9wd.aspx). Mind that this new column will need a **new, unique** name, different from others. So, if you have a 'BaseCostHighWay' that is double, that new one must have different name. But, remember that "name" is just an identifier. It is the column's Header that is displayed. – quetzalcoatl Jun 06 '13 at 09:33
0

Have you tried DataColumn? DataColumn Class

Tim
  • 28,212
  • 8
  • 63
  • 76
Quake
  • 1
0
crow["BaseCostHighWay * POF * PTF * WCF"] = 
    String.Format("{0},{1},{2},{3}", BaseCostScoreHW, POF, PTF, WCF);
David
  • 15,894
  • 22
  • 55
  • 66
SamiHuutoniemi
  • 1,576
  • 2
  • 16
  • 35