我是vb的新手,无法解决我的这个问题。我从datagridview标记这个列有红色框:
从上面的图像我想要一个像这样的输出:
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..
如果字符串以“TEAM”开头,我已经有了代码检测代码,它只是想要的输出中的差异。到目前为止,这是我的代码:
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
这个输出是:
Team Edna - Esca, Adarayan, Dianne
Team Aennah, Adarayan, Dianne
Team Edna, Bacalla, Catherine
Team Aennah, Bacalla, Catherine //so on..
请帮助我的家伙。
答案 0 :(得分:2)
试试这个..:替换你的代码,
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
<强>输出:强>
HTH ..:)
答案 1 :(得分:2)
此示例还包括其他项目......您可以使用数组或将其发送到messagebox,richtextbox或者您想要的任何内容......
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
这是输出的样子......