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

时间:2014-12-07 15:55:24

标签: vb.net loops datagridview

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

这是我编写的一个小样本程序,用于测试方法。

    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

1 个答案:

答案 0 :(得分:0)

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

DataGridView是一个VIEW对象,应该这样使用。没有理由像你一样手动添加行。首先获取并组织您的数据,然后将其绑定到DGV,它会更有效率,特别是当您有很多行时。

此外,在这种情况下添加一行并没有多大意义。您应该做的是使用分组和聚合,但默认情况下DataGridView不支持分组。但是有一些扩展名,例如http://10tec.com/articles/datagridview-grouping-two-recipes.aspx

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

假设这个源文件:

"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

结果:

enter image description here