VB.NET column copy and paste in datagridview

问题描述 投票:0回答:0

我的问题是将复制的列粘贴到在 datagridview 上使用相同值选择的多个列。我有一个 screenshot 的结果,我想执行就像一个相同的值复制到多个列。

这是我在 VB 中的代码:

私有子粘贴值

 Dim clipboardContent As String = Clipboard.GetText()
    Dim selectedCells As DataGridViewSelectedCellCollection = Trxn_AwardListDataGridViewX.SelectedCells
    Dim rowCount As Integer = Trxn_AwardListDataGridViewX.Rows.Count
    Dim colCount As Integer = Trxn_AwardListDataGridViewX.Columns.Count

    If Not selectedCells.Count > 0 Then
        Return
    End If

    Dim selectedColumns As New List(Of Integer)()

    For Each cell As DataGridViewCell In selectedCells
        If Not selectedColumns.Contains(cell.ColumnIndex) Then
            selectedColumns.Add(cell.ColumnIndex)
        End If
    Next

    Dim currentRowIndex As Integer = selectedCells(0).RowIndex

    Dim rowValues As String() = clipboardContent.Split({ControlChars.CrLf}, StringSplitOptions.RemoveEmptyEntries)

    For i As Integer = 0 To rowValues.Length - 1
        If currentRowIndex >= rowCount Then
            Exit For
        End If

        Dim cellValues As String() = rowValues(i).Split({ControlChars.Tab}, StringSplitOptions.None)

        For j As Integer = 0 To cellValues.Length - 1
            Dim columnIndex As Integer = selectedColumns(j)

            If columnIndex >= colCount Then
                Exit For
            End If

            Dim cellValue As String = cellValues(j)
            Dim decimalValue As Decimal

            If Decimal.TryParse(cellValue, decimalValue) Then
                Trxn_AwardListDataGridViewX(columnIndex, currentRowIndex).Value = decimalValue
            Else
                Trxn_AwardListDataGridViewX(columnIndex, currentRowIndex).Value = cellValue
            End If
        Next

        currentRowIndex += 1
    Next

结束子

vb.net datagridview pasting
© www.soinside.com 2019 - 2024. All rights reserved.