2017-10-05 5 views
0

내 양식 : 나는 데이터 세트에서 선택한 행을 삭제하려면삭제하고 데이터 세트에서 업데이트 행 (C#을)

enter image description here

. 버튼 "삭제"는이 핸들러가 있습니다 만 DataGridView를 삭제

  int indexForDeleting = 0; 
      bool reason = true; 
      foreach (DataGridViewRow item in this.dataGridView1.SelectedRows) 
      { 
       this.dataGridView1.Rows.RemoveAt(item.Index); 
       if (reason) 
       { 
        indexForDeleting = item.Index; 
        reason = false; 
       } 
      } 
      dataSet11.flight.Rows.Remove(dataSet11.flight.Rows[Math.Abs(indexForDeleting)]); 
      dataSet11.flight.AcceptChanges(); 

선택한 행을,하지만 난 DataGridView에와 데이터 세트에서 행을 삭제합니다.

답변

0

해당 표가 DataTable에 바인딩되어 있으면 표 행 대신 해당 테이블의 DataRow를 삭제하십시오. 이 같은 것 :

int rowIdx = dataGridView1.CurrentCell.RowIndex; 
DataRowView drv = (DataRowView)dataGridView1.Rows[rowIdx].DataBoundItem; 
DataRow dr = drv.Row; 
dr.Delete(); 

이렇게하면 행이 삭제되고 그리드도 업데이트됩니다.

+0

이 문제를 해결하지 못합니다. 행은 데이터 그 림뷰 만 삭제합니다. – bsuart

+0

예. DataRow.Delete()는 행의 RowState 속성을 Deleted로 설정합니다. 일반적으로 어댑터를 사용하여 해당 DataSet/DataTable을 데이터베이스로 업데이트하면 RowState에 따라 행을 추가/업데이트/삭제합니다. –