2016-10-20 8 views
1

WinForms를 사용하고 있습니다. '$'기호가있는 경우 가격 열의 값을 어떻게 추가합니까? 열에 "$"기호가 없을 때 값을 추가 할 수 있지만 시스템에서 오류가 발생합니다.열에서 모든 값 추가 DataGridView

이것은 내가 지금까지 가지고있는 것입니다. 이렇게하면 price 열의 모든 값이 추가되고 '$'기호가 포함되지 않은 경우 합계됩니다.

private void sum_Btn_Click(object sender, EventArgs e) 
{ 
    Total_TxtBx.Text = (from DataGridViewRow row in dataGridView1.Rows 
         where row.Cells[2].FormattedValue.ToString() != string.Empty 
         select Convert.ToDecimal(row.Cells[2].FormattedValue)).Sum().ToString(); 
} 

($ 1.00 + $ 2.00 + $ 3.00) 전체 텍스트 상자해야 평등 = $ 6.00

enter image description here

+0

첫 번째 뒤에 다음을 추가하십시오. where :'where! row.Cells [3]. FormattedValue. ToString(). ("$")'가 들어 있습니까? – TaW

+0

'$'기호는 데이터의 일부가 아니어야합니다. 컬럼의 데이터는 숫자 데이터 타입이어야하지만,'Format'은'$'기호를 포함해야합니다. –

답변

1

하나가 다른 과부하로 값을 구문 분석 할 필요가 Convert any currency string to double이 질문에서 볼 수 있듯이 :

string x = "$3.00"; 
var result = decimal.Parse(x, NumberStyles.Currency); 

// result = 3.00 

코드에서 :

private void sum_Btn_Click(object sender, EventArgs e) 
{ 
    Total_TxtBx.Text = (from DataGridViewRow row in dataGridView1.Rows 
         where row.Cells[2].FormattedValue.ToString() != string.Empty 
         select decimal.Parse(row.Cells[2].FormattedValue, NumberStyles.Currency)) 
         .Sum().ToString(); 
} 
+0

다른 것을하려고합니다. 나는 가격 행을 합산하려고 노력하고있다. 그러나 각 열에 $ 기호가 있기 때문에 시스템에서 오류가 발생합니다. – taji01

+0

@ taji01 - 업데이트보기 –

+0

시스템에서'NumberStyles.Currency'를 사용할 수 없기 때문에 이것을 사용할 수 없습니다. 'using System.Globalization'을 시도했지만'row.Cells [2] .FormattedValue'는 에러를 던집니다. – taji01