2016-04-13 69 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

啊,是的,感謝它現在正在工作我知道我錯過了一些東西,只是不能把它放在手指上。非常感謝 :) –