Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Expert

I had a for loop and i am getting one string value at all the time of execuing the string value.
How to append the string. Code pasted below. Kindly provide your solutions.

VB
For Each row As GridViewRow In gvmstrgrid.Rows
                Dim chk As CheckBox = DirectCast(row.FindControl("chkbx1"), CheckBox)
                If chk.Checked = True Then
                    Dim strbr As New StringBuilder
                    Dim final As String
                    Dim str1 As String
                    str1 = DirectCast(row.FindControl("lblamc"), Label).Text

                End If


From the above code i want the str1 to be concatenate and show the final result in another string.

Rgds
Jagadesh

[edit]Removed shouting in subject and added code formatting.[/edit]
Posted
Updated 24-Jun-13 22:34pm
v2

I would use a string builder object to achieve this:

VB
Dim s As New StringBuilder
Dim final As String
For Each row As GridViewRow In gvmstrgrid.Rows
     Dim chk As CheckBox = DirectCast(row.FindControl("chkbx1"), CheckBox)
     If chk.Checked = True Then

        Dim str1 As String
        str1 = DirectCast(row.FindControl("lblamc"), Label).Text
        s.Append(str1)

      End If
Next

final = s.ToString()


Something like that I believe will do what you are after.
Please note where I moved your declarations of the string builder and the final string to.
 
Share this answer
 
Comments
Thomas Daniels 25-Jun-13 4:41am    
+5!
Pheonyx 25-Jun-13 4:43am    
Thanks :-)
VB
Dim str1 As String = string.Empty
For Each row As GridViewRow In gvmstrgrid.Rows
    Dim chk As CheckBox = DirectCast(row.FindControl("chkbx1"), CheckBox)
    If chk.Checked = True Then
        Dim strbr As New StringBuilder
        Dim final As String
        str1 &= DirectCast(row.FindControl("lblamc"), Label).Text & VBNewLine 'or & ", "
    End If
Next

Happy Coding!
:)
 
Share this answer
 
Comments
Thomas Daniels 25-Jun-13 4:47am    
Reason for my vote of 1: string concatenation should be avoided. Use StringBuilder instead:
What's so bad about string concatenation?
use
VB
str1 += DirectCast(row.FindControl("lblamc"), Label).Text


and finally, you will be able to find string concatanation in this
VB
str1 
string type.
 
Share this answer
 
Comments
Thomas Daniels 25-Jun-13 4:48am    
Reason for my vote of 1: string concatenation should be avoided. Use StringBuilder instead:
What's so bad about string concatenation?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900