2016-07-08 1 views
0

저는 C#으로 Windows Form 응용 프로그램을 작성 중입니다.필터링 된 DataGridView를 DataRow []에 할당하여 null 값을 지정합니다 (필터 결과 있음)

양식을 추가/삭제할 수있는 DataGridView입니다. 입력란은 편집 가능 Qty 열이며 그 다음 Save 버튼입니다. Save 클릭하면

, 나는 다른 절약을 진행, 목록에 0.00 Qty이다, 0.00 QtyDataGridView 항목을 필터링 한 후 /이 있음을 사용자에게 통지합니다.

DataRow[] result = (enrollmedsDataGridView.DataSource as DataTable).Select("Qty = 0.00 OR Qty = 0"); 
:

private void SaveBtn_Click(object sender, EventArgs e) 
    { 
     if (isWithZeroQty() == true) 
     { 
      MessageBox.Show("Please check Quantity","System Alert",MessageBoxButtons.OK,MessageBoxIcon.Warning); 
     } 
     else 
     { 
      // Will do the saving.. 
     } 
    } 

    private bool isWithZeroQty() 
    { 
     DataRow[] result = (enrollmedsDataGridView.DataSource as DataTable).Select("Qty = 0.00 OR Qty = 0"); 

     if (result.Count() > 0) 
     { return true; } 
     else 
     { return false; } 
    } 

내 문제가 NullReferenceException이 줄에 발생했습니다 :

Form where the user add/delete entry and set the Qty

내가 양식에 코드를 아래이 있습니다 (저장을 클릭하기 전에 양식의 스크린 샷 참조)

추가 조사를 위해 DataGridView를 캐스팅 할 때 DataTable (enrollmedsDataGridView.DataSource as DataTable)으로 NullReferenceException이 발생합니다.

DataGridView가 BindingSource에 대한 DataBounded이기 때문에 그렇습니까?

그렇다면 어떻게 해결할 수 있습니까?

도움을 미리 감사드립니다.

답변

0

위의 코드에서 NullReferenceException이 발생하는 이유는 여전히 찾을 수 없습니다.

그러나 해결 방법으로

대신 필터링과 DataTableDataGridView 주조 나는 내 말에 잘 작동 0 or 0.00 값이있는 경우 셀 Qty을 확인하기 위해 모든 행에 foreach 문을 사용했다.

private bool isWithZeroQty() 
    { 
     int zeros = 0; 
     foreach (DataGridViewRow row in enrollmedsDataGridView.Rows) 
     { 
      if ((row.Cells["Qty"].Value.ToString() == "0") || (row.Cells["Qty"].Value.ToString() == "0.00")) 
      { 
       zeros = 1; 
      } 
     } 
     if (zeros > 0) 
     { return true; } 
     else 
     { return false; } 
    } 
:

코드 아래 참조