2011-12-22 5 views
2

Google에이 도구를 사용해 봤지만 나에게 적합한 솔루션을 찾을 수 없었습니다.DataGrid에서 행을 편집 할 때 감지합니다.

나는 클라이언트가 알지 못하는 SQL 테이블의 정보를 표시하는 DataGrid를 가지고 있습니다. 클라이언트는 서버에 요청을 보내고 응답으로 <SomeClass> List를 가져 와서 DataGrid에 표시합니다.

사용자가 행을 변경하고 사용자가 입력 한 새로운 값이 필요할 때이를 감지해야합니다. 현재 RowEditEnding 이벤트를 사용하고 있습니다. 이 이벤트를 처리하는 메서드는 다음과 같습니다.

private void editRowEventHandler(object sender, DataGridRowEditEndingEventArgs e) 
{ 
    SomeClass sClass = e.Row.DataContext as SomeClass; 
    // Send sClass to the server to be saved in the database... 
} 

이렇게하면 편집중인 행이 표시됩니다. 그러나 그것은 변경 전의 행을 제공하고 변경이 발생한 후에 행을 가져 오는 방법을 파악할 수 없습니다.

내가 이것을 할 수있는 방법을 알고 있거나 내가 알아낼 수있는 방향으로 나를 가리킬 수있는 사람이 여기 있니?

+0

왜 그냥 SomeClass 집합에 잡으려고하지 않습니까? – Paparazzi

답변

1

경우에 따라 개체의 변경 사항을 감지하려고합니다.

resultGrid.CellEditEnding += resultGrid_CellEditEnding; 
void resultGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) 
     { 
      var yourClassInstance = e.EditingElement.DataContext; 
      var editingTextBox = e.EditingElement as TextBox; 
      var newValue = editingTextBox.Text; 
     } 

을 : 그것은 대신

귀하의 데이터 그리드가 resultGrid입니다 가정, 내가 아래의 코드로 올 "행"에 따라서 당신이 "셀"에 초점을 맞출 필요는 SomeClass의 특성에 온다 "e"에는 셀의 행과 열에 대한 정보도 들어 있습니다. 따라서 셀이 사용하는 편집기를 알 수 있습니다. 이 경우 텍스트 상자라고 가정합니다. 도움이 되길 바랍니다.

+0

새로운 값으로 행을 한 줄로 얻을 수 있기를 바랬습니다. 왜냐하면 20 개의 멤버 변수를 가진 클래스가 있기 때문입니다. 그래서 나는 내가 사용하고있는 열을보고 정확한 값을 변수에 할당하는 스위치를 작성해야 할 것 같다. 필자가 작성한 것보다 더 많은 코드가 있지만 문제가 해결됩니다. 감사합니다. :) – Laleila

3

셀 단위로 읽지 않으려면 here 토론을 참조하십시오.

private void OnRowEditEnding(object sender, DataGridRowEditEndingEventArgs e) 
{ 
    DataGrid dataGrid = sender as DataGrid; 
    if (e.EditAction == DataGridEditAction.Commit) { 
     ListCollectionView view = CollectionViewSource.GetDefaultView(dataGrid.ItemsSource) as ListCollectionView; 
     if (view.IsAddingNew || view.IsEditingItem) { 
      this.Dispatcher.BeginInvoke(new DispatcherOperationCallback(param => 
      { 
       // This callback will be called after the CollectionView 
       // has pushed the changes back to the DataGrid.ItemSource. 

       // Write code here to save the data to the database. 
       return null; 
      }), DispatcherPriority.Background, new object[] { null }); 
     } 
    } 
} 
관련 문제