2014-06-14 2 views
2

CellValidating 및 RowValidating 이벤트가 처리 된 DataGridview가 있습니다. 내 문제는 그것을 클릭하면 내 마음이 바뀌고 현재 행의 편집을 취소하려는 경우 이러한 유효성 검사 방법은 내가 할 수 있도록하지 않을 것입니다. 첫 번째 행 인 경우 이미 유효한 일부 경우 작동하는 것 같습니다. 행이 있지만 첫 번째 일 때는 포커스를 다른 것으로 바꿀 수 없습니다.사용자가 DataGridview에서 편집을 취소 할 수 있도록 허용

내 질문에 어떻게 검증 및 datagridview 행의 편집을 취소 할 수 있습니까? 편집을 취소 할 때 datagridview의 현재 행이 손실되면 상관 없습니다.

편집 : 여기 내 확인하는 방법 :

여기
private void Neighbours_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) 
     { 
      if (e.ColumnIndex == 2) 
      { 
       int i = 0; 
       if (!int.TryParse(e.FormattedValue.ToString(), out i) || i <= 0) 
       { 
        e.Cancel = true; 
        Neighbours.Rows[e.RowIndex].ErrorText = "error"; 
       } 
      } 
     } 

private void Neighbours_RowValidating(object sender, DataGridViewCellCancelEventArgs e) 
     { 
      DataGridViewRow r = ((DataGridView)sender).Rows[e.RowIndex]; 
      int dist = 0; 
      if (r.Cells["NeighboursStop1"].Value == null || r.Cells["NeighboursStop1"].Value.ToString() == "" || r.Cells["NeighboursStop2"].Value == null || r.Cells["NeighboursStop2"].Value.ToString() == "") 
      { 
       e.Cancel = true; 
       r.ErrorText = "error"; 
      } 
      else if (r.Cells["NeighboursStop1"].Value.ToString() == r.Cells["NeighboursStop2"].Value.ToString()) 
      { 
       e.Cancel = true; 
       r.ErrorText = "error"; 
      } 
      else if(!int.TryParse(r.Cells["NeighboursDistance"].Value.ToString(), out dist)) 
      { 
       e.Cancel = true; 
       r.ErrorText = "error"; 
      } 
      else if (dist <= 0) 
      { 
       e.Cancel = true; 
       r.ErrorText = "error"; 
      } 
      else 
      { 
       r.ErrorText = ""; 
      } 
     } 

가 발생했을 때 그것이 내가 키 탈출 충돌하는 경우, 오류 메시지가 사라지지만, 모습입니다 나는 다른, RowValidation 뭔가를 클릭 할 때 다시이 상태로 다시 I 일 :

error

EDIT2 : 나는이 DataGridView에 대한 데이터 소스로 DataTable을 사용합니다.

+0

몇 가지 코드와 예제를 제공하십시오. – codemonkey

+0

데이터 소스로 무엇을 사용하고 있습니까? – Tuco

+0

데이터 테이블을 데이터 소스로 사용합니다. – hynner

답변

2

코드를 테스트 할 수 없지만 다음을 시도해 볼 수 있습니까?

private void Neighbours_RowValidating(object sender, DataGridViewCellCancelEventArgs e) 
    { 

     //Include this, check to see if the row is dirty 
     if (Neighbours.Rows[e.RowIndex] != null && !Neighbours.Rows[e.RowIndex].IsNewRow && Neighbours.IsCurrentRowDirty) 
     { 



     DataGridViewRow r = ((DataGridView)sender).Rows[e.RowIndex]; 
     int dist = 0; 
     if (r.Cells["NeighboursStop1"].Value == null || r.Cells["NeighboursStop1"].Value.ToString() == "" || r.Cells["NeighboursStop2"].Value == null || r.Cells["NeighboursStop2"].Value.ToString() == "") 
     { 
      e.Cancel = true; 
      r.ErrorText = "error"; 
     } 
     else if (r.Cells["NeighboursStop1"].Value.ToString() == r.Cells["NeighboursStop2"].Value.ToString()) 
     { 
      e.Cancel = true; 
      r.ErrorText = "error"; 
     } 
     else if(!int.TryParse(r.Cells["NeighboursDistance"].Value.ToString(), out dist)) 
     { 
      e.Cancel = true; 
      r.ErrorText = "error"; 
     } 
     else if (dist <= 0) 
     { 
      e.Cancel = true; 
      r.ErrorText = "error"; 
     } 
     else 
     { 
      r.ErrorText = ""; 
     } 
    } 

} 
+0

완벽하게 작동합니다. – hynner

관련 문제