增量更新 DataGridView 单元格

Updating A DataGridView Cell Incrementally

我目前在复制一行和递增序列号时遇到一个小问题。

所以基于按钮点击,这就是我复制第 0 行的方式,每次点击只复制一次。

    Dim dr As DataRow

    For n As Integer = 0 To 0         ' how many dupes you want
        dr = tbl.NewRow
        For c As Integer = 0 To tbl.Columns.Count - 1   ' copy data from 0 to NewRow
            dr.Item(c) = tbl.Rows(0).Item(c)
        Next
        tbl.Rows.Add(dr)            ' add NewRow to datatable
    Next n

这是我创建序列号的方法,用前导零填充,它似乎递增,但只有在我单击复制按钮之后,所以基本上添加的最后一行,它是重复的第 0 行,但不是' t代表需要的新序列号。

'UPDATE SEQUENCE NUMBER
    i += 1

    Dim value As Integer = i
    Dim r As Integer

    Dim decimalLength1 As Integer = value.ToString("D").Length + 7
    Dim decimalLength2 As Integer = value.ToString("D").Length + 6
    Dim decimalLength3 As Integer = value.ToString("D").Length + 5
    Dim decimalLength4 As Integer = value.ToString("D").Length + 4

    If i >= 0 And i <= 9 Then
        '1 TO 9 FORMAT
        DataGridView1.CurrentCell = DataGridView1.CurrentRow.Cells("sequence")
        DataGridView1.Item(73, r).Value = value.ToString("D" + decimalLength1.ToString())
        'Debug.Print(value.ToString("D" + decimalLength1.ToString()))
    ElseIf i >= 10 And i <= 99 Then
        '10 TO 99 FORMAT
        DataGridView1.CurrentCell = DataGridView1.CurrentRow.Cells("sequence")
        DataGridView1.Item(73, r).Value = value.ToString("D" + decimalLength2.ToString())
        'Debug.Print(value.ToString("D" + decimalLength1.ToString()))
    ElseIf i >= 100 And i <= 999 Then
        '100 TO 999 FORMAT
        DataGridView1.CurrentCell = DataGridView1.CurrentRow.Cells("sequence")
        DataGridView1.Item(73, r).Value = value.ToString("D" + decimalLength3.ToString())
        'Debug.Print(value.ToString("D" + decimalLength1.ToString()))
    ElseIf i >= 1000 And i <= 9999 Then
        '1000 TO 9999 FORMAT
        DataGridView1.CurrentCell = DataGridView1.CurrentRow.Cells("sequence")
        DataGridView1.Item(73, r).Value = value.ToString("D" + decimalLength4.ToString())
        'Debug.Print(value.ToString("D" + decimalLength1.ToString()))
    End If

第 0 行的序号始终为 1,因此理论上我需要从 2 开始递增。

建议?有 better/cleaner 的方法吗?

更新 2

    Dim startSeq As Integer = Convert.ToInt32(tbl.Rows(0).Item(73))

    MsgBox("startSeq = " & startSeq)

    For n As Integer = 0 To NumericUpDown1.Value - 1

        MsgBox("n = " & n)

        dr = tbl.NewRow

        For c As Integer = 0 To tbl.Columns.Count - 1

            dr.Item(c) = tbl.Rows(0).Item(c)

            If c = "73" Then     ' if this is the SEQ column,
                ' add the current seq val to the seq column
                dr.Item(c) = (startSeq + n).ToString("00000000")
            End If
        Next c
        tbl.Rows.Add(dr)
    Next n

看起来您应该能够在创建副本时添加音序器。也许使它成为一种方法并传递具有序列字符串的列的索引。类似于:

Private Sub DuplicateRows(ColIndx As Integer,
                Dupes As Integer)

    ' start value is Row(0) + 1
    Dim startSeq As Integer = Convert.ToInt32(tbl.Rows(0).Item(ColIndx )) + 1

    For n As Integer = 0 to Dupes -1
        dr = tbl.NewRow

        For c As Integer = 0 To tbl.Columns.Count - 1
             If c = ColIndx Then     ' if this is the SEQ column,
                ' add the current seq val to the seq column
               dr.Item(c) = (startSeq + n).ToString("00000000")
           Else
               ' otherwise copy the data from Row(0)
               dr.Item(c) = tbl.Rows(0).Item(c) 
           End If
        Next c
        tbl.Rows.Add(dr)
    Next n
End Sub

这应该用递增的计数器初始化每个新行。 Is there a better/cleaner way of doing this

a) 你应该添加到 DataTable,而不是 DGV,如果它是绑定的

b) (startSeq + n).ToString("00000000") 应该做填充等而不是那个丑陋的代码块。

c) 使用 Option Strict OnIf c = "73" ... 是胡说八道,让编译器猜测你的意图。它是虫子食物。

d) 硬编码“73”可能 这次 有效,但之前您说过它可以在任何地方。下面的代码根据名称查找序列列,因此它可以出现在任何地方。您可以在制作 dupes 之前甚至在 Dupes 过程中找到它,而不是表单级别的 var。

e) Dim startSeq As Integer = Convert.ToInt32(tbl.Rows(0).Item(73)) 如果您检查上面的答案,这应该是 ... + 1 以增加第一个值。


用法:

Private tbl As DataTable             ' table loaded from flat file
Private SeqColIndex As Integer       ' assigned somewhere to
                                   ' point to the "sequence" column

' method to load data
Dim connstr = "Provider=Microsoft.ACE.OLEDB.12.0;..."
Using cn As New OleDbConnection(connstr)
  ...
End Using

' FIND the sequence column for this session
For n = 0 To tbl.Columns.Count - 1
    If tbl.Columns(n).ColumnName.ToLowerInvariant = "sequence" Then
        SeqColIndex = n
        Exit For
    End If
Next

' later to add some rows
Private Sub ButtonAddRow_Click(...

    DuplicateRows(SeqColIndex, NumericUpDown1.Value)
End Sub