计算datagridview中的重复项并显示重复项数

时间:2014-05-09 18:15:36

标签: vb.net datagridview duplicates

您好我正在尝试计算未绑定数据网格中的重复数量。

我的问题是,当我清除我的字符串然后开始计数一个新的计数并解除之前的重复条目,但如果我不清除字符串我得到重复数字作为我的字符串中的第二个数字这是好的但我需要它搜索重复项并在列

中显示该类别的数量或重复项

我按下按钮。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        DataGridView2.ColumnCount = 7

        DataGridView2.Columns(0).Name = ""
        DataGridView2.Columns(1).Name = ""
        DataGridView2.Columns(2).Name = ""
        DataGridView2.Columns(3).Name = ""
        DataGridView2.Columns(4).Name = ""
        DataGridView2.Columns(5).Name = ""
        DataGridView2.Columns(6).Name = "UID"

        Dim row As String() = New String() {"", "", "", "", "", "", "" & Label1.Text & "" & Label4.Text & " " & Label3.Text & " " & Label5.Text & " " & Label6.Text & ""}
        DataGridView2.Rows.Add(row)

Timer7.Start()

End Sub

然后这个

Private Sub Timer7_Tick(sender As Object, e As EventArgs) Handles Timer7.Tick
  Dim date1 As String = DateTime.Now.ToString("dd MMMM yyyy")
    Form3.DataGridView1.ColumnCount = 7
    Form3.DataGridView1.Columns(0).Name = "SERIAL"
    Form3.DataGridView1.Columns(1).Name = "PORT"
    Form3.DataGridView1.Columns(2).Name = "Attempt"
    Form3.DataGridView1.Columns(3).Name = "PEAK IM3"
    Form3.DataGridView1.Columns(4).Name = "Date of Data"
    Form3.DataGridView1.Columns(5).Name = "Time of Data"
    Form3.DataGridView1.Columns(6).Name = "UID"
    Dim row As String() = New String() {"" & Label1.Text & "", "" & Label4.Text & " " & Label3.Text & " " & Label5.Text & " " & Label6.Text & "", "" & Label15.Text & "", "", date1, CStr(TimeOfDay), "" & Label1.Text & "" & Label4.Text & " " & Label3.Text & " " & Label5.Text & " " & Label6.Text & ""}
    Form3.DataGridView1.Rows.Add(row)

    Dim dc As New Dictionary(Of String, Integer)()
    Dim clV As String

    For i As Integer = 0 To Form3.DataGridView1.Rows.Count - 1

        If Not Form3.DataGridView1.Rows(i).IsNewRow Then

            clV = Me.DataGridView2(6, i).Value.ToString()

            If Not dc.ContainsKey(clV) Then
                dc.Add(clV, +1)
            Else
                dc(clV) += 1
            End If
        End If
    Next

    Dim sb As New StringBuilder()

    For Each kvp As KeyValuePair(Of String, Integer) In dc
        sb.AppendLine(String.Format("{0} ", kvp.Value))
    Next

    Label15.Text = sb.ToString()

    Timer7.Stop()
End Sub

我花了两天的时间试图解决这个问题,并且需要一些建议。

1 个答案:

答案 0 :(得分:0)

解决:

必须将我的数据绑定到数据库数据表。

然后我用它来在工具条中制作一个过滤按钮。

过滤column_heading LIKE? +'%'

然后使用上面的代码给出了正确的计数。

步骤如下

  1. 向DGV添加数据

  2. 保存到DGV到数据库

  3. 过滤

  4. 统计结果

  5. 刷新DGV