I have a StringBuilder I am using and I add to it from a loop that goes through a DataGridView. What I need to do is separate the strings if there are more than one with a “, “. Then I set a label’s text from the StringBuilder. Below is a working example without the comma’s…
THIS IS THE UPDATED VERSION THAT WORKS GREAT NOW….
Dim strUnits As New System.Text.StringBuilder
Dim lineCount As Integer = 0
For i = 0 To dgvLowInventory.RowCount - 1
If dgvLowInventory.Rows(i).Cells(1).Value Is DBNull.Value Or dgvLowInventory.Rows(i).Cells(2).Value Is DBNull.Value Then
'Skip
Else
If lineCount >= 1 Then
strUnits.Append(", ")
strUnits.Append("[" & dgvLowInventory.Rows(i).Cells(1).Value & " - " & dgvLowInventory.Rows(i).Cells(3).Value & "]")
lineCount += 1
Else
strUnits.Append("[" & dgvLowInventory.Rows(i).Cells(1).Value & " - " & dgvLowInventory.Rows(i).Cells(3).Value & "]")
lineCount += 1
End If
End If
Next
lblTestString.Text = strUnits.ToString()
Keep a counter, starting at 0.
Add 1 to the counter every time you append, after you append.
Before appending, if the counter is greater than 0, append a
", "before appending the string.Alternatively, use String.Join with
", ", depending on what language you are programming in there’s usually an equivalent to this.http://msdn.microsoft.com/en-us/library/57a79xd0.aspx