2016-04-13 71 views
-2

我所做的应用程序有一个名为服务成本的文本框。这允许用户输入他们提供的服务的成本,这是十进制的。我试图让这个服务费用显示在DGV中,除此之外,我还有其他一切工作。字符串到十进制

currentComputer.ServiceCost = Convert.ToDecimal(txtServiceCost); 

上面是我目前有的代码,有没有我在这里完成的错误?

+3

应该是什么错呢?它不工作吗? *如何*不起作用?任何错误消息? –

+1

你有没有使用WinForms? – Ian

+0

请提供您收到的错误?另外txtServiceCost是一个文本框?您应该首先阅读该文本框中的文本。什么是DGV? –

回答

1

从问题很明显,txtServiceCostTextBox,并Convert.ToDecimal()需要一个string作为输入,所以你应该使用txtServiceCost.Text而不是为txtServiceCost。由于txtServiceCost是一种控制,其中作为txtServiceCost.Text是一个字符串

currentComputer.ServiceCost = Convert.ToDecimal(txtServiceCost.Text); 

但我想建议你使用decimal.TryParse

decimal userInput; 

if (!decimal.TryParse(txtServiceCost.Text, out userInput)) 
{ 
    // Throw some warning here that invalid input 
} 

currentComputer.ServiceCost = userInput; 
+0

啊,是的,感谢它现在正在工作我知道我错过了一些东西,只是不能把它放在手指上。非常感谢 :) –