Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Dynamic CheckBoxList Creation

Dynamic CheckBoxList Creation

Scheduled Pinned Locked Moved Visual Basic
cssdatabasehelpquestionannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GaryT80
    wrote on last edited by
    #1

    I am trying to get the Page_load event to dynamically update my checkboxlist values to correspond with the columns in my grid view. the code I have is as follows: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim listItemCount As Integer = -1 Dim totalColNum As Integer = GridView1.Columns.Count If Page.IsPostBack Then ''Do Something Else While listItemCount <= totalColNum listItemCount += 1 CheckBoxList2.Items.Add(GridView1.Columns(listItemCount).ToString) End While End If End Sub But I am getting this error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Any ideas??

    Q G 2 Replies Last reply
    0
    • G GaryT80

      I am trying to get the Page_load event to dynamically update my checkboxlist values to correspond with the columns in my grid view. the code I have is as follows: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim listItemCount As Integer = -1 Dim totalColNum As Integer = GridView1.Columns.Count If Page.IsPostBack Then ''Do Something Else While listItemCount <= totalColNum listItemCount += 1 CheckBoxList2.Items.Add(GridView1.Columns(listItemCount).ToString) End While End If End Sub But I am getting this error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Any ideas??

      Q Offline
      Q Offline
      quacks_a_lot
      wrote on last edited by
      #2

      Try replacing '<=' with '<' in 'While listItemCount <= totalColNum'. If this doesn't fix it put a break inside the loop and see just how many times you are actually looping through before the error occurs. When the program reaches the break you can hold the mouse over the 'totalColNum' variable and see how many times its trying to loop.

      1 Reply Last reply
      0
      • G GaryT80

        I am trying to get the Page_load event to dynamically update my checkboxlist values to correspond with the columns in my grid view. the code I have is as follows: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim listItemCount As Integer = -1 Dim totalColNum As Integer = GridView1.Columns.Count If Page.IsPostBack Then ''Do Something Else While listItemCount <= totalColNum listItemCount += 1 CheckBoxList2.Items.Add(GridView1.Columns(listItemCount).ToString) End While End If End Sub But I am getting this error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Any ideas??

        G Offline
        G Offline
        GaryT80
        wrote on last edited by
        #3

        I figured it out:

        Protected Sub Page\_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim listItemCount As Integer = -1
            Dim totalColNum As Integer = GridView1.Columns.Count
            Dim valNum As Integer = -1
            If Not Page.IsPostBack Then
                totalColNum = totalColNum - 1
                While listItemCount < -1
                    listItemCount += 1
                    valNum += 1
                    CheckBoxList2.Items.Add(GridView1.Columns(listItemCount).ToString)
                    CheckBoxList2.Items.Item(listItemCount).Value = valNum.ToString
                End While
            End If
        End Sub
        
        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups