We help IT Professionals succeed at work.

stringbiulder help for gridview

Wigging
Wigging asked
on
Medium Priority
224 Views
Last Modified: 2012-05-12
     I need to build a comma separated string out of the quantity and the revisions
for instance, if qty2 = 2 on first line  and  1 on second line then the string will look like

b,b,a

because my table looks like

revision  |  qty
b                2
a                1


my code so far for gridview

 For Each dr2 In gridview2.Rows
                Dim revision2 As System.Web.UI.WebControls.Label = dr2.FindControl("lblrevision")
                Dim qty2 As System.Web.UI.WebControls.TextBox = dr2.FindControl("txtqty")

            Next
Comment
Watch Question

Commented:
Dim revision As System.Web.UI.WebControls.Label
Dim qty As System.Web.UI.WebControls.TextBox
Dim sb As New StringBuilder()

For Each dr As DataRow in gridView2.Rows
   revision =  = dr.FindControl("lblrevision")
   gty= dr.FindControl("txtqty")

   for i as Int to qty+1
       If sb.Lenght > 0 Then sb.Append(",")

       sb.Append(revision)
   loop
Next

Author

Commented:
loop?  im getting an error

Commented:
sorry, cross-language issue. "loop" should be "Next"

Author

Commented:
This is what I want to accomplish but i get syntax error and "to" is highlighted?

    For Each dr2 In gridview2.Rows
                Dim revision2 As System.Web.UI.WebControls.Label = dr2.FindControl("lblrevision")
                Dim qty2 As System.Web.UI.WebControls.TextBox = dr2.FindControl("txtqty")
                Dim sb As New StringBuilder()
                Dim g As Integer
                   for g  to  qty2.text+1
                    If sb.Length > 0 Then sb.Append(",")

                    sb.Append(revision2)
                Next
            Next
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
change

for g  to  qty2.text+1

to

for g = 0 to  qty2.text+1

Author

Commented:
Ok this is my end result........  Session("append") = A,A,A  ?????
what I need is  for scenario below

B.B,A,A

My table

revision   qty
B              2
A               2


my code


        For Each dr2 In gridview2.Rows
                Dim revision2 As System.Web.UI.WebControls.Label = dr2.FindControl("lblrevision")
                Dim qty2 As System.Web.UI.WebControls.TextBox = dr2.FindControl("txtqty")
                Dim sb As New StringBuilder()
                Dim g As Integer
                For g = 0 To qty2.Text
                    If sb.Length > 0 Then sb.Append(",")

                    Session("append") = sb.Append(revision2.Text).ToString

                Next
            Next
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
Move this line to outside of loop

Dim sb As New StringBuilder()

Change this

 Session("append") = sb.Append(revision2.Text).ToString

to

sb.Append(revision2.Text)


The add this after the loop

 Session("append") = sb.ToString

Author

Commented:
you are the king

Explore More ContentExplore courses, solutions, and other research materials related to this topic.