0

I need to get selected items on checkbox and set as string format like (value1,value2,value3) from the checkbox i selected

For Each row As GridViewRow In GridView1.Rows    
    If row.RowType = DataControlRowType.DataRow Then
        Dim CheckRow As CheckBox = (TryCast(row.Cells(1).FindControl("chckSelector"), CheckBox))

        If CheckRow.Checked Then
            Dim scode As String = TryCast(row.Cells(2).FindControl("lblsstorecode"), Label).Text
            lbltest.Text = 'this i want to get the value like this (value1,value2,value3) from checkbox that i selected                
        End If
    End If
Next
12
  • From your previous post you had a list of checkboxes right? Why don't you loop it and store the value of the checked ones? Commented Sep 16, 2019 at 7:03
  • I'm sorry but i don't know how to do that.. Can you please show me what im going to do Commented Sep 16, 2019 at 7:06
  • I'm not on my pc to give proper code but you should do this: Create a list of string, make a for each chk as checkbox in checklist and check if chk.checked = True then list.add(chk.text), try this approach Commented Sep 16, 2019 at 7:10
  • Ok sir im trying to do it. Commented Sep 16, 2019 at 7:17
  • Or you can do something that might be better, you can use CheckBox.CheckedChanged to add or remove the value from the list, it's up to you Commented Sep 16, 2019 at 7:19

1 Answer 1

1

Depending on why you are using the GridView control, you might me able to accomplish this easier by using a CheckBoxList instead. In Asp.net what you describe is accomplished very easily with a CheckBoxList as follows:

In .aspx:

<asp:CheckBoxList ID="Itemlst" runat="server" RepeatColumns="1">
    <asp:ListItem Value="">Item 1</asp:ListItem>
    <asp:ListItem Value="">Item 2</asp:ListItem>
    <asp:ListItem Value="">Item 3</asp:ListItem>
</asp:CheckBoxList>

</br>
<asp:Button ID="Button1" runat="server" Text="Button" />
</br>
<asp:TextBox ID="TextBox1" runat="server" Width="400"></asp:TextBox>

Then in the code behind:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim selected = New List(Of ListItem)
    Dim itemcount As Integer
    Dim csvlist As String = ""
    For Each item As ListItem In Itemlst.Items
        If item.Selected Then selected.Add(item)
    Next

    itemcount = selected.Count
    For i = 0 To itemcount - 1
        csvlist = csvlist & selected(i).ToString & ","
    Next
    csvlist = Mid(csvlist, 1, Len(csvlist) - 1)
    TextBox1.Text = csvlist
End Sub

The Windows Forms CheckedListBox would probably work similar if you are developing a desktop application.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.