2012-02-14 6 views
0

내가 System.InvalidOperationException이 처리되지 않은했다 동일한 데이터 그리드 뷰 컨트롤의 행 메시지 = 대처하면서 예외를 다음과 점점 오전에 속한다 "이미 제공된 행이 DataGridView 컨트롤에 속해 있습니다." 다음 사본 내가 빈 문자열로 번째 열을 유지하려는 붙여 넣기 방법에 붙여 넣기 동안 DataGridViewSelectedRowCollection예외 : 이미 제공된 행은 DataGridView 컨트롤

copy() 
    { 
      If (DataGridViewWorkGroupDetails.Rows.Count = 1) Then 
       Exit Sub 
      End If 
      Try 
       pasteMode = "copy" 
       currentRowCollection = DataGridViewWorkGroupDetails.SelectedRows 
      Catch ex As Exception 
       MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "frmworkgroup:copyRowCollectionError") 
      End Try 
     } 

로 붙여 넣기 방법

paste() 
{ 
    Dim row As DataGridViewRow 
    Dim myRow As DataGridViewRow 
     For Each row In currentRowCollection 
      myRow = row 
      myRow.Cells.Item(1).Value = String.Empty 
      DataGridViewWorkGroupDetails.Rows.Insert(DataGridViewWorkGroupDetails.Rows.Count - 1, myRow) 
     Next 

} 

currentRowCollection에서 행을 선택하는 복사 방법입니다. 한 datagridview에서 다른 행으로 복사 할 때 작동하지만 동일한 datagridview로 복사하면 예외가 추가됩니다. 제공된 행이 이미 DataGridView 컨트롤에 속해 있습니다.

답변

0

데이터row에 대한 참조를 사용하고 있습니다.

의 행
For Each row In currentRowCollection 

실제로는 이미 데이터 뷰에있는 행에 대한 참조입니다.

삽입을 호출하기 전에 행에있는 데이터의 전체 복사본을 만들어야합니다.

myRow = DataGridViewWorkGroupDetails.NewRow(); 
myRow.ItemArray = row.ItemArray; 
DataGridViewWorkGroupDetails.Rows.Add(myRow); 

이 코드를 테스트하지 않은,하지만이

+0

당신이 나에게 그것 vb.net ..There에서 작동하지 않는 그 –

+0

에 대한 샘플 코드를 제공시겠습니까 작동합니다은에 대한 NewRow() 메소드입니다 위 라인에서 사용 된 datagridview myRow = DataGridViewWorkGroupDetails.NewRow(); –

관련 문제