2010-01-11 2 views
4

DataGridView.IsCurrentRowDirtytrue입니다. false으로 설정하여 포커스를 잃을 때 RowValidating을 트리거하지 않습니다.데이터베이스 변경 내용을 적용한 후에 변경 내용을 커밋 한 후에 DataGridView 행이 여전히 더럽습니다.

저는 BindingList<T>에 연결된 DataGridView입니다. 나는 CellEndEdit 이벤트를 처리하고 데이터베이스에 변경 사항을 저장합니다. 변경 사항을 저장 한 후 행의 모든 ​​셀이 최신이기 때문에 DataGridView.IsCurrentRowDirtytrue으로 설정하고 싶습니다. 그러나 그것은 false으로 설정됩니다.

행이 포커스를 잃을 때 RowValidating이 트리거되어 3 개의 셀을 모두 처리하고 유효성을 검사하기 때문에 문제가 발생합니다. 따라서 모든 셀이 유효하고 아무 것도 더티가 아닌 경우에도 여전히 유효성을 검사합니다. 그건 낭비입니다. 여기

내가 무슨의 예 :

void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) 
{ 
    // Ignore cell if it's not dirty 
    if (dataGridView.isCurrentCellDirty) 
     return; 

    // Validate current cell. 
} 

void dataGridView_RowValidating(object sender, DataGridViewCellCancelEventArgs e) 
{ 
    // Ignore Row if it's not dirty 
    if (!dataGridView.IsCurrentRowDirty) 
     return; 

    // Validate all cells in the current row. 
} 

void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
{ 
    // Validate all cells in the current row and return if any are invalid. 

    // If they are valid, save changes to the database 

    // This is when I would expect dataGridView.IsCurrentRowDirty to be false. 
    // When this row loses focus it will trigger RowValidating and validate all 
    // cells in this row, which we already did above. 
} 

내가 폼의 Validate() 메서드를 호출 할 수 있다고 말했다 게시물을 읽었습니다,하지만 그건 내가 노력하고있어, 이는 화재 RowValidating의 원인이됩니다 기피.

어떻게 설정할 수 있습니까? DataGridView.IsCurrentRowDirtytrue으로 설정 하시겠습니까? 아니면 RowValidating이 불필요하게 모든 셀의 유효성을 검사하지 못하게하는 방법일까요?

답변

3

데이터를 데이터베이스에 저장 한 후 DataGridView1.EndEdit()을 호출 해 보았습니까?

+0

그래, 그게 작동하지 않았다. RowValidating은 행이 포커스를 잃을 때도 계속 발생합니다. 툴팁은'EndEdit()'이 현재 셀에서 작동한다고 말합니다. 비슷한 것이 있다면, EndRowEdit처럼 운이 좋을 수도 있습니다. 그래도 제안을 주셔서 감사합니다! – Ecyrb

+2

누군가가 나를 좋아하는 사람이 있다면. 나는 이것을'datagrid.currentrow.datagrid.endedit()','datagrid.endedit' 그리고 마지막으로'bindingSource.endedit()'호출하여 해결했다. – ppumkin

0

Validating과 같은 문제가 두 번 발생했습니다. 편집하기 전에 한 번 편집 (예상대로),하지만 내가 생각하는 다시에 DataGridView 초점을 변경합니다.

조사 할 시간이 없었습니다. 빠른 수정이 this.ActiveControl = null; 아직 의도하지 않은 결과가 있는지 확실하지 않지만 프로그래밍 방식으로 컨트롤을 unfocussing하여 유효성 검사 문제를 해결합니다.

private void cntrl_MethodParameters_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) 
{ 
    //Init 
    var dgv = (DataGridView)sender; 
    int row = e.RowIndex; 
    int col = e.ColumnIndex; 

    //Validate Edit 
    if ((row >= 0) && (col == cntrl_MethodParameters.Columns.IndexOf(cntrl_MethodParameters.Columns[MethodBuilderView.m_paramValueCol]))) 
    { 
     string xPropertyName = (string)cntrl_MethodParameters[MethodBuilderView.m_paramNameCol, row].EditedFormattedValue; 
     string xPropertyValue = (string)cntrl_MethodParameters[MethodBuilderView.m_paramValueCol, row].EditedFormattedValue; 
     bool Validated = FactoryProperties.Items[xPropertyName].SetState(xPropertyValue); 

     //Cancel Invalid Input 
     if (!Validated) 
     { 
      dgv.CancelEdit(); 
     } 
    } 
    this.ActiveControl = null; 
} 
관련 문제