将数据粘贴到DataGridView vb.net

时间:2019-01-16 08:48:43

标签: vb.net datagridview copy-paste

所以今天早上我碰到了一堵墙:

使用此代码,我粘贴数据时会随机获得另一行显示。

它旨在接收1到99999之间的值

所以当我复制此内容时:

然后将其粘贴到程序中

https://outlook.office365.com/owa/service.svc?action=CreateFolder&EP=1&ID=-47&AC=1

1 个答案:

答案 0 :(得分:0)

下面是我的解决方案。您必须从事件中调用函数。在我的应用程序中,我需要决定是否允许PasteUnboundRecords子项粘贴,该子项由Allowed boolen

控制
Public Sub CopyRows(MyDataGridView As DataGridView)
    Dim d As DataObject = MyDataGridview.GetClipboardContent()
    Try
        Clipboard.SetDataObject(d)
    Catch
        MessageBox.Show("Text not copied, null value")
    End Try
End Sub

Sub PasteUnboundRecords(MyDataGridView As DataGridView)
    Dim Allowed As Boolean      
    Dim OriginLines As String() = Clipboard.GetText(TextDataFormat.Text).Split(New String(0) {vbCr & vbLf}, StringSplitOptions.None)
    Dim Xo As Integer = SelectedAreaMinColumn(MyDataGridview)
    Dim Yo As Integer = SelectedAreaMinRow(MyDataGridview)
    Dim X As Integer
    Dim Y As Integer
    Dim i As Integer
    Dim j As Integer
    Dim ii As Integer
    Dim jj As Integer = 0
    Dim m1 As Integer
    Dim n1 As Integer = OriginLines.Length  
    Dim m2 As Integer = SelectedAreaColumns(MyDataGridView)
    Dim n2 As Integer = SelectedAreaRows(MyDataGridview)
    Dim m2Max As Integer = MyDataGridview.Columns.Count
    Dim n2Max As Integer = MyDataGridview.Rows.Count

    For j = 0 To Math.Max(n1-1, n2-1)
        Y = Yo + j
        If Y = n2Max Then Exit For
        If j-jj*n1 = n1 Then jj = jj+1
        Dim OriginValue As String() = OriginLines(j-jj*n1).Split(New String(0) {vbTab}, StringSplitOptions.None)
        m1 = OriginValue.Length
        ii = 0
        For i = 0 To Math.Max(m1-1, m2-1)
            X = Xo + i
            If X = m2Max Then Exit For
            If i-ii*m1 = m1 Then ii = ii+1
            If X > 0 Then 'Avoid pasting in first column containing codes
                If Y = 0 Then 'Avoid first line
                    Allowed = True
                Else 'Check DataValidatios
                    If DataValidations(OriginValue(i-ii*m1), X) = "OK" Then
                        Allowed = True
                    Else
                        Allowed = False
                    End If
                End If
                'Avoid pasting in Readonly columns
                If MyDataGridview.Rows(Y).Cells(X).ReadOnly Then
                    Allowed = False
                End If
                If Allowed Then
                    MyDataGridview.Rows(Y).Cells(X).Value = OriginValue(i-ii*m1)
                    End If                                  
                End If
            End If
        Next i
    Next j
End Sub

Private Function SelectedAreaMinRow(MyDataGridView As DataGridView) As Integer
    Dim minRowIndex As Integer
    For i As Integer = 0 To MyDataGridView.SelectedCells.Count - 1
        If i = 0 Then
            minRowIndex = MyDataGridView.SelectedCells.Item(i).RowIndex
        End If
        minRowIndex = Math.Min(MyDataGridView.SelectedCells.Item(i).RowIndex, minRowIndex)
    Next i

    Return minRowIndex
End Function

Private Function SelectedAreaMinColumn(MyDataGridView As DataGridView) As Integer
    Dim minColumnIndex As Integer
    For i As Integer = 0 To MyDataGridView.SelectedCells.Count - 1
        If i = 0 Then
            minColumnIndex = MyDataGridView.SelectedCells.Item(i).ColumnIndex
        End If
        minColumnIndex = Math.Min(MyDataGridView.SelectedCells.Item(i).ColumnIndex, minColumnIndex)
    Next i

    Return minColumnIndex
End Function

Private Function SelectedAreaRows(MyDataGridView As DataGridView) As Integer
    Dim minRowIndex As Integer
    Dim MaxRowIndex As Integer
    For i As Integer = 0 To MyDataGridView.SelectedCells.Count - 1
        If i = 0 Then
            minRowIndex = MyDataGridView.SelectedCells.Item(i).RowIndex
            MaxRowIndex = MyDataGridView.SelectedCells.Item(i).RowIndex
        End If
        minRowIndex = Math.Min(MyDataGridView.SelectedCells.Item(i).RowIndex, minRowIndex)
        MaxRowIndex = Math.Max(MyDataGridView.SelectedCells.Item(i).RowIndex, MaxRowIndex)
    Next i

    Return MaxRowIndex-minRowIndex+1
End Function

Private Function SelectedAreaColumns(MyDataGridView As DataGridView) As Integer
    Dim minColumnIndex As Integer
    Dim MaxColumnIndex As Integer
    For i As Integer = 0 To MyDataGridView.SelectedCells.Count - 1
        If i = 0 Then
            minColumnIndex = MyDataGridView.SelectedCells.Item(i).ColumnIndex
            MaxColumnIndex = MyDataGridView.SelectedCells.Item(i).ColumnIndex
        End If
        minColumnIndex = Math.Min(MyDataGridView.SelectedCells.Item(i).ColumnIndex, minColumnIndex)
        MaxColumnIndex = Math.Max(MyDataGridView.SelectedCells.Item(i).ColumnIndex, MaxColumnIndex)
    Next i

    Return MaxColumnIndex-minColumnIndex+1
End Function