vb.net 中 datagridview 列中的数据循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21472091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:37:40  来源:igfitidea点击:

loop of data within datagridview column in vb.net

vb.netloopsdatagridviewforeach

提问by Aljie

i'm new in vb and can't solve this problem of mine. I have this column from datagridview mark with red box:

我是 vb 新手,无法解决我的这个问题。我有来自 datagridview 标记的此列,带有红色框:

enter image description here

在此处输入图片说明

And from the above image i want an output like this:

从上面的图像中,我想要这样的输出:

Team Edna - Esca, Adarayan, Dianne, //first shout
Team Edna - Esca, Bacalla, Catherine //2nd shout and so on..
Team Aennah, Aquino, Juan Benigno //3rd shout
Team Aennah, Aguila, Mailebeth //4rth shout and so on..

I already had the code that code detech if the string is starting with "TEAM" it just the discrepancy in the wanted output. Here's my code so far:

如果字符串以“TEAM”开头,我已经有了编码 detech 的代码,它只是所需输出中的差异。到目前为止,这是我的代码:

 For i As Integer = 1 To Me.DataGridView1.Rows.Count
            For Each row As DataGridViewRow In DataGridView1.Rows
                If Not row.IsNewRow Then
                    If InStr(row.Cells(0).Value.ToString.ToUpper, UCase(Trim("TEAM"))) <> 0 Then
                        team = row.Cells(0).Value.ToString.ToUpper
                        MessageBox.Show(team)
                        teamMembers = Me.DataGridView1.Rows(i).Cells(0).Value.ToString()
                        MessageBox.Show(team + ", " + teamMembers)
                    End If
                End If
            Next
        Next

And the output of this was:

这个输出是:

Team Edna - Esca, Adarayan, Dianne
Team Aennah, Adarayan, Dianne
Team Edna, Bacalla, Catherine
Team Aennah, Bacalla, Catherine //so on..

Please help my guys.

请帮助我的家伙。

回答by Unknownymous

try this.. : replace your code ,

试试这个..:替换你的代码,

 Dim team, groups, teamMembers As String
        Dim _counter As Integer = 0

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

            If DataGridView1.Rows(_xdtRow).Cells(0).Value.ToString.Contains("Team") = True Then

                team = DataGridView1.Rows(_xdtRow).Cells(0).Value.ToString

                '_counter = _xdtRow

                For _xrow As Integer = (_xdtRow + 1) To DataGridView1.Rows.Count - 1

                    _counter += 1

                    If DataGridView1.Rows(_xrow).Cells(0).Value.ToString.Contains("Team") = True Then
                        Exit For
                    Else
                        teamMembers += DataGridView1.Rows(_xrow).Cells(0).Value.ToString & ","
                    End If

                Next

                _xdtRow = _counter

                groups += team & " - " & teamMembers & vbNewLine & vbNewLine

                'MsgBox(groups)

                team = Nothing
                teamMembers = Nothing

            End If
        Next

         Dim _perGrp As String()
    _perGrp = groups.Split(New Char() {"_"c})

    For Each perGrp As String In _perGrp
        MsgBox(perGrp)
    Next

OUTPUT:

输出:

enter image description here

在此处输入图片说明

HTH.. :)

HTH .. :)

回答by ???ěxě?

This example ALSO includes you other items as well... you have arrays that you can play around with or send it to messagebox, richtextbox or what ever you want with it...

这个例子还包括你其他的项目......你有数组,你可以使用它或将它发送到消息框、富文本框或任何你想要的......

 Private Sub btnOutput_Click(sender As Object, e As EventArgs) Handles btnOutput.Click
    Dim arrTeamOne As New ArrayList
    Dim arrTeamTwo As New ArrayList
    Dim strMembers As New StringBuilder
    Dim blnTeamOne As Boolean = False

    For i As Integer = 0 To dgvMembers.Rows.Count - 1
        If Not (dgvMembers.Rows(i).Index = 0) Then
            If dgvMembers.Rows(i).Cells(0).Value.ToString.Contains("Team Edna - Esca") Then
                Continue For
            Else
                If Not dgvMembers.Rows(i).Cells(0).Value.ToString.Contains("Team Aennah") Then
                    If Not blnTeamOne Then
                        arrTeamOne.Add("Team Edna - Esca, " & dgvMembers.Rows(i).Cells(0).Value.ToString & ", " & dgvMembers.Rows(i).Cells(1).Value.ToString & ", " & dgvMembers.Rows(i).Cells(2).Value.ToString)
                    Else
                        arrTeamTwo.Add("Team Aennah, " & dgvMembers.Rows(i).Cells(0).Value.ToString & ", " & dgvMembers.Rows(i).Cells(1).Value.ToString & ", " & dgvMembers.Rows(i).Cells(2).Value.ToString)
                    End If
                Else
                    If dgvMembers.Rows(i).Cells(0).Value.ToString.Contains("Team Aennah") Then
                        blnTeamOne = True
                        Continue For
                    End If
                End If
            End If
        End If
      Next

    For Each arr As String In arrTeamOne
        strMembers.AppendLine(arr.ToString)
    Next
    For Each arr As String In arrTeamTwo
        strMembers.AppendLine(arr.ToString)
    Next
    Messagebox.Show(strMembers.ToString)
End Sub

Here's what the output looks like...

这是输出的样子......

enter image description here

在此处输入图片说明