2016-04-15 4 views
0

私はvb./netに取り組んでいます。 グリッドビューの場合Like +と - ユーザーがクリックした後に1つの行が最後の位置に追加する必要があり、ユーザーがクリックすれば1行が現在の位置から削除する必要があります最後の位置に行を追加し、現在の位置から行を削除する方法VB.netのDataGridviewで

私は次のコードを試してみましたが、私にエラー

Private count As Integer = 1 
    Private Sub dgvSourcePath_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvSourcePath.CellContentClick 
     If e.ColumnIndex = 2 Then 
      dgvSourcePath.Rows.Insert(count) 
     End If 
     If e.ColumnIndex = 3 Then 
      count -= 1 
      dgvSourcePath.Rows.RemoveAt(count) 
     End If 
     If e.ColumnIndex = 1 Then 
      OpenSourceFileDialog.ShowDialog() 
      If Windows.Forms.DialogResult.OK Then 
       dgvSourcePath.CurrentRow.Cells(0).Value = OpenSourceFileDialog.FileName 
      End If 
     End If 
     dgvSourcePath.Refresh() 
    End Sub 
+0

正しくコードをフォーマットしてください。 – James

+0

エラーメッセージが何であるか、そしてエラーが発生した行を正確に特定できる方法があれば、 – jmcilhinney

+0

'CellContentClick'イベントは、クリックされた行のインデックスを返します。行を削除するときは、その索引を使用する必要があります。追加については、 'Add'が特にコレクションの最後に追加されるときに' Insert'を使うのはなぜですか? – jmcilhinney

答えて

0

を与えるこれを試してください:

 Private Sub dgvdatos_CellContentClick(sender As DataGridView, e As DataGridViewCellEventArgs) Handles dgvdatos.CellContentClick 
Try 

    dgvdatos.EndEdit() 

    If TypeOf (sender.CurrentCell) Is DataGridViewButtonCell Then 

    If e.ColumnIndex = 2 Then 
     dgvdatos.Rows.Insert(sender.RowCount - 1) 
    End If 
    If e.ColumnIndex = 3 And sender.Rows.Count > 1 Then 
     count -= 1 
     dgvdatos.Rows.RemoveAt(e.RowIndex) 
    End If 
    If e.ColumnIndex = 1 Then 
     OpenSourceFileDialog.ShowDialog() 
     If DialogResult.OK Then 
     dgvdatos.CurrentRow.Cells(0).Value = OpenSourceFileDialog.FileName 
     End If 
    End If 
    dgvdatos.Refresh() 
    End If 
Catch ex As Exception 

    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
End Try 

End Subの

+0

ありがとうございます。 –

関連する問題