Populating Check Box List via Code

Posted on 2010-01-04
Last Modified: 2013-11-26
Trying to assign values from a dataset to a checkbox list control. I create the dataset (ds), fill it, then use the following line of code in a loop:

CheckBoxList1.Items.Insert(0, ds.Tables(0).Rows(X).Item(1)).ToString()

This code populates the CheckBox List with the correct items. However, I want to associate a selected value (integer) with the item (string). the value is at ds.Tables(0).Rows(X).Item(2)) so, for example, one option has selectedItem of Pencil, while the selectedValue of pencil is 2. The one line of code, as written, fills the Value and Item properties with the same data.

Question by:john03063
    LVL 51

    Expert Comment

    try this:

    CheckBoxList1.Items.Insert(0, new ListItem(ds.Tables(0).Rows(X).Item(1)).ToString(), ds.Tables(0).Rows(X).Item(2)).ToString())
    LVL 51

    Accepted Solution

    for excample this is what I tested and working fine

            CheckBoxList1.Items.Insert(0, New ListItem("select a city", 0))
            CheckBoxList1.Items.Insert(0, New ListItem("Istanbul", 1))
            CheckBoxList1.Items.Insert(0, New ListItem("Ankara", 2))
            CheckBoxList1.Items.Insert(0, New ListItem("Gaziantep", 3))

    first item is the text, the second is the value when selected

    Author Closing Comment

    Thanks! I knew it had to be a simple thing!!


    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now