Aljie
Aljie

Reputation: 190

loop of data within datagridview column in vb.net

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

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:

 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.

Upvotes: 0

Views: 19893

Answers (2)

Trevor
Trevor

Reputation: 8004

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

Upvotes: 2

rayncorg
rayncorg

Reputation: 993

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.. :)

Upvotes: 2

Related Questions