Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 在datagridview中插入行的不同方式是什么_Vb.net_Loops_Datagridview - Fatal编程技术网

Vb.net 在datagridview中插入行的不同方式是什么

Vb.net 在datagridview中插入行的不同方式是什么,vb.net,loops,datagridview,Vb.net,Loops,Datagridview,我有一个程序,在这个程序中,我将文本文件中的行读取到datagridview中,该文件是从我的预算程序导出的。然后,我想在每周之后插入一行,总计一周。我找到的唯一方法是使用DataGridView1.Rows.Count。问题是,假设这个表有3000行,它将插入1000行。使用计数方法,在插入1000行之后,它将在第2000行的顶部总共计数3000行。因为插入的2000+1000=总数为3000。似乎计数方法正在查看行的索引,每次插入一行时,索引都会增加一 下面是我编写的一个小示例程序,用于测试

我有一个程序,在这个程序中,我将文本文件中的行读取到datagridview中,该文件是从我的预算程序导出的。然后,我想在每周之后插入一行,总计一周。我找到的唯一方法是使用DataGridView1.Rows.Count。问题是,假设这个表有3000行,它将插入1000行。使用计数方法,在插入1000行之后,它将在第2000行的顶部总共计数3000行。因为插入的2000+1000=总数为3000。似乎计数方法正在查看行的索引,每次插入一行时,索引都会增加一

下面是我编写的一个小示例程序,用于测试该方法

    Dim RowValue() As String = {"Line 1", "Line 2", "Line 3", "Line 4", "Line 5", "Line 6", "Line 7", "Line 8", "Line 9", "Line 10"}
    For x = 0 To 9
        DataGridView1.Rows.Insert(x, RowValue(x))
    Next

    Dim y As Integer = 0
    Dim RowCount As Integer = DataGridView1.Rows.Count - 1
    For count = 1 To RowCount Step 2
        Dim InsertValue() As String = {"Insert 1", "Insert 2", "Insert 3", "Insert 4", "Insert 5", "Insert 6", "Insert 7", "Insert 8", "Insert 9", "Insert 10"}
        DataGridView1.Rows.Insert(count)
        DataGridView1.Rows(count).Cells(1).Value = InsertValue(y)
        y += 1
    Next
如您所见,我在运行时创建了10行,并尝试插入10行,但它只插入5行。每次插入一行时,我都会尝试将1添加到RowCount中,但对它插入行的方式没有任何影响

是否有另一种插入行的方法可以让我一直插入到datagridview的末尾。差不多

Do Until (last row of datagridview)
    (do something here)
Loop

你的问题中信息太少,无法正确回答,但这里有一些提示

DataGridView是一个视图对象,应该这样使用。没有理由像现在这样手动添加行。首先获取并组织数据,然后将其绑定到DGV,这样效率更高,尤其是当您有很多行时

此外,在这种情况下添加一行也没有多大意义。您应该使用分组和聚合,但是默认情况下DataGridView不支持分组。但是也有一些扩展,例如类似于这些

如果您不想进行分组,您应该做的是在数据源中添加一个额外的列,并将总计放在其中

假设此源文件:

"RowId","RowAmount","WeekId"
1,1,1
2,2,1
3,3,1
4,4,2
5,5,2
6,6,2
7,7,3
8,8,3
9,9,3
这就是我要做的,但请记住,无论是视觉上还是功能上,分组都要好得多

Private Sub LoadDataInDgv()
    Dim sourceFilename As String = "c:\source.txt"

    Dim dt As DataTable = ReadCsv(sourceFilename, ",", True, "WeekId ASC, RowId ASC")

    AddWeeklyTotals(dt)

    DataGridView1.DataSource = dt
End Sub

Private Sub AddWeeklyTotals(ByRef dt As DataTable)
    If dt.Rows.Count = 0 Then Exit Sub

    Dim row As Integer = 0
    Dim previousWeekId = CInt(dt.Rows(row)("WeekId"))

    dt.Columns.Add("WeeklyTotal", GetType(Double))

    Dim runningTotal As Double = 0

    For row = 0 To dt.Rows.Count - 1
        Dim currentWeekId = CInt(dt.Rows(row)("WeekId"))
        Dim rowAmount = CDbl(dt.Rows(row)("RowAmount"))

        If currentWeekId <> previousWeekId Then
            dt.Rows(row - 1)("WeeklyTotal") = runningTotal
            runningTotal = rowAmount
            previousWeekId = currentWeekId
        Else
            runningTotal += rowAmount
        End If

        If row = dt.Rows.Count - 1 Then dt.Rows(row)("WeeklyTotal") = runningTotal

    Next
End Sub


Private Function ReadCsv(ByVal filename As String, ByVal delimiter As Char, ByVal hasHeaders As Boolean, ByVal orderByExp As String) As DataTable
    Dim result As New DataTable

    Dim fi As New System.IO.FileInfo(filename)

    Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""" & fi.DirectoryName & """;Extended Properties='text;HDR=" & If(hasHeaders, "Yes", "No") & ";FMT=Delimited(" & delimiter & ")';"
    Dim cmdStr As String = "SELECT * FROM [" & fi.Name & "]" & If(String.IsNullOrWhiteSpace(orderByExp), "", " ORDER BY " & orderByExp) & ";"

    Using Conn As New System.Data.OleDb.OleDbConnection(connStr)
        Using Cmd As New System.Data.OleDb.OleDbCommand(cmdStr, Conn)
            Using Adapter As New System.Data.OleDb.OleDbDataAdapter(Cmd)
                Adapter.Fill(result)
            End Using
        End Using
    End Using

    Return result
End Function
结果是:


我真的不喜欢使用扩展名,但不在行中添加,只需将总数添加到总计列即可。谢谢