2012-01-26 5 views
1

이것이 쉬운 방법 일 것이라고 생각합니다.캐스팅/캐스팅하지 않을 때의 무한 값 오류

if (((int)row.Cells["Pareto"].Value <= 50) && (row.Cells["Pareto"].Value != null)) 

이 코드는 캐스팅이 무한 수일 때 오류가 발생합니다.

필자도 시도 :

if (((int)row.Cells["Pareto"].Value <= 50) && ((string)row.Cells["Pareto"].Value != null)) 
if (((int)row.Cells["Pareto"].Value <= 50) && (row.Cells["Pareto"].Value != "")) 

어떤 도움도 좋을 것!

Im Visual Studio 10에서 C#을 사용하여 winforms를 사용합니다.

오류 : 당신의 관심이 경우 코드의

더 "숫자에서 캐스팅 할 때 값이 무한대보다 작아야합니다"

foreach (DataGridViewRow row in dataGridView1.Rows) 
     { 
      //countpg 
      //countPg++; 
      pgtothold = Convert.ToSingle(row.Cells["Qty"].Value); 
      countPg += pgtothold; 

      //if (((int)row.Cells["Pareto"].Value <= 50) && (row.Cells["Pareto"].Value != null)) 
      if ((Convert.ToDouble(row.Cells["Pareto"].Value)) && ((int)row.Cells["Pareto"].Value <= 50) 
      { 
       //top 50 
       //top++; 
       tempTop = Convert.ToSingle(row.Cells["Qty"].Value); 
       top += tempTop; 
      } 
      else 
       if (((int)row.Cells["Pareto"].Value > 50) && ((int)row.Cells["Pareto"].Value <= 100) && (row.Cells["Pareto"].Value != "")) 
       { 
        //50-100 
        tempMidt = Convert.ToSingle(row.Cells["Qty"].Value); 
        tmid += tempMidt; 
       } 
       else 
+2

어떤 정확한 오류가 발생합니까? 또한 캐스트하기 전에 null을 확인하려고합니다. 바로 가기 평가에서 작업을 수행하려면 – TJHeuvel

+2

(row.Cells [ "Pareto"] .Value! = null)이 가장 먼저 표시되어야합니다. –

+1

정수로 캐스팅 한 후 null을 확인합니다. 먼저 문을 재정렬하여 null을 먼저 확인하십시오. – Broam

답변

1

내가 사용하는 것이 좋습니다 : (이 허용되는 경우) 내용이 DBNull이 경우 0으로 실패합니다

var data = System.Convert.ToInt32(row.Cells["Pareto"].Value); 
    if(data <=50) 
    { 
    ---- 
    } 

, 그래서 당신의 코드가 깨끗하고 안전한 발생합니다.

+0

그 뜻은 메신저 null을 int로 변환 뜻이야? 그러므로 무한한가? – lemunk

+0

@StevenSmith 제안 된 코드를 시도해보고 작동하는지 확인하십시오. Convert.ToDouble (...)하려고하지 않는 경우. –

+0

현재 오류가 발생하여 int를 bool로 변환하는 중임 – lemunk

3
if ((row.Cells["Pareto"].Value != null) && ((int)row.Cells["Pareto"].Value <= 50)) 

난 당신이 원하는 생각합니다. 물건이 null의 경우, AND의 나머지를 평가하지 않습니다.

+0

이미 동일한 오류가 발생했습니다. – lemunk

+0

여러 개의 if로 나누어 정확히 무슨 일이 일어나는지 확인하십시오. 문제가 값이 null이면이 작동합니다. 그래서 다른 일이 일어나야합니다. – Almo

0

데이터 테이블의 결과는 십진 유형의 가능성이 큽니다. 따라서 이것을 시도하십시오 :

if (((decimal)row.Cells["Pareto"].Value <= 50.0m) && (row.Cells["Pareto"].Value != null)) 
관련 문제