如何将重复字符从Datagridview计算到文本中?

时间:2015-11-17 20:18:22

标签: vb.net ms-access datagridview

这就是我的datagridview的样子:

ID |名称|当然|
1 | AB | IT
2 | CD | IT
3 | EF | CS
4 | GH | BA
5 | IJ | IT

它假设获取列“course”的重复字符串的值。所以,结果假设看起来像这样:

(标签)IT = 3(标签)
(标签)CS = 1(标签)
(标签)BA = 1(标签)
(标签)IST = 0(标签)


这是我的代码。

 Private Sub CounterButton_Click(sender As System.Object, e As System.EventArgs) Handles CounterButton.Click

    Dim countertry As String = 0
    Dim result As String = 0
    Dim IT As Integer = 1
    Dim IST As String = 1
    Dim CS As String = 1
    Dim BA As String = 1

    For c As Integer = 0 To StudentInfoTableDataGridView.RowCount - 1

        countertry += StudentInfoTableDataGridView.Rows(c).Cells(3).Value

        ' Im so lost here

        ' If IT it will show IT = +1
        ' If IS it will show IST = +1
        ' If CS it will show CS = +1
        ' If BA it will show BA = +1

    Next
    CountText1.Text = result '(for IT)
    CountText2.Text = result '(for IST)
    CountText3.Text = result '(for CS)
    CountText4.Text = result '(for BA)

End Sub

3 个答案:

答案 0 :(得分:0)

您可以尝试这种方法。这取决于您如何填充数据表。但是你可以尝试创建一个字典,然后将datagrid与字典进行比较

Find duplicates in a Datagridview

答案 1 :(得分:0)

我认为如果当前字符串与键相同,您可以使用Dictionary并添加项值。

像这样的东西

Private Sub CounterButton_Click(sender As System.Object,e As System.EventArgs)处理CounterButton.Click

Dim result As String = 0
Dim courseDict As new Dictionary<string, integer>
Dim currString As String

For c As Integer = 0 To StudentInfoTableDataGridView.RowCount - 1

    currString = StudentInfoTableDataGridView.Rows(c).Cells(3).Value

    If courseDict.ContainsKey(currString) Then
       courseDict(currString) += 1
    Else
       courseDict.Add(currString,0)
    End If


Next
CountText1.Text = currString("IT")
CountText2.Text = currString("IST")'(for IST)
CountText3.Text = currString("CS")'(for CS)
CountText4.Text = currString("BA")'(for BA)

End Sub

答案 2 :(得分:0)

以下按课程和地点划分的行会将结果分组到标签中。我已经评论了有关标签名称的代码,以便工作。另请注意,我通过DataGridView的设计器创建了列。

''' <summary>
''' Columns for DataGridView are defined in the IDE
''' There is one label per count where labels are named
''' Label'ss' e.g. LabelIT, LabelCS etc
''' </summary>
''' <remarks></remarks>
Public Class Form1
    Private labelList As New List(Of Label)
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.Rows.Add(New Object() {1, "AB", "IT"})
        DataGridView1.Rows.Add(New Object() {2, "CD", "IT"})
        DataGridView1.Rows.Add(New Object() {3, "EF", "CS"})
        DataGridView1.Rows.Add(New Object() {4, "GH", "BA"})
        DataGridView1.Rows.Add(New Object() {5, "IJ", "IT"})

        labelList = New List(Of Label) _
            From {LabelIT, LabelCS, LabelBA, LabelIST}

    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim labelItem As Label = Nothing
        ' set all label text to zero in the event
        ' one or more labels are not in a grouping
        For Each lb In labelList
            lb.Text = "0"
        Next

        ' one liner to group by last column and provide count
        Dim results =
            (
                From row In DataGridView1.Rows.Cast(Of DataGridViewRow)()
                Where Not row.IsNewRow) _
                .GroupBy(Function(row) row.Cells("CourseColumn").Value) _
                .Select(
                    Function(iGroupRow)
                        Return New With
                               {
                                   Key .Name = iGroupRow.Key,
                                       .Count = iGroupRow.Count
                               }
                    End Function)

        For Each item In results
            labelItem = labelList.Where(
                Function(lb) lb.Name.EndsWith(item.Name.ToString)
            ).FirstOrDefault

            If labelItem IsNot Nothing Then
                labelItem.Text = item.Count.ToString
            End If
        Next
    End Sub
End Class