Solved

getting selected checkedlistbox text values c sharp/Linq

Posted on 2011-03-14
8
1,214 Views
Last Modified: 2012-05-11
can someone tell me how to get the values from a checkedlistbox selected items ?
Please dont send me a link I have tried them all without success.
Error attached

does not give title
thanks in advance
private void AddPerson_Load(object sender, EventArgs e)
        {
            DSZorkDataContext db = new DSZorkDataContext();
            var disabilitylist = from p in db.LookUps
                                 where p.Category == "cat1"
                                 select p;


            this.checkedListBox1.DataSource = db.LookUps;
            this.checkedListBox1.DisplayMember = "Description";
            this.checkedListBox1.ValueMember = "Description"; 
         


        }
   private void simpleButton2_Click(object sender, EventArgs e)
        {
                              foreach (int indexChecked in checkedListBox1.CheckedIndices)
            {
                // The indexChecked variable contains the index of the item.
                MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" +
                                checkedListBox1.GetItemCheckState(indexChecked).ToString() + ".");
            }

            // Next show the object title and check state for each item selected.
            foreach (object itemChecked in checkedListBox1.CheckedItems)
            {

                // Use the IndexOf method to get the index of an item.
                MessageBox.Show("Item with title: \"" + itemChecked.ToString() +
                                "\", is checked. Checked state is: " +
                                checkedListBox1.GetItemCheckState(checkedListBox1.Items.IndexOf(itemChecked)).ToString() + ".");
            }

Open in new window

0
Comment
Question by:martin05
  • 3
  • 3
  • 2
8 Comments
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 35134310

foreach( object item in checkedListBox1.CheckedItems )
{
    MessageBox.Show( ((LookUp)item).Description );
}

Open in new window

0
 
LVL 11

Expert Comment

by:Kusala Wijayasena
ID: 35134325
Try this:

foreach (DataRowView dr in checkedListBox1.CheckedItems)
{
    //Display ValueMember
    MessageBox.Show(dr[checkedListBox1.ValueMember].ToString());
}

Open in new window


-Kusala
0
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 35134342
DataRowView is not the type of the CheckedItem. If he had used a DataTable it would be, but he used a LINQ query to get a collection of objects as his DataSource, so the type of the CheckedItem will be a LookUp (based on the line of code that shows he is accessing the LookUps collection of his context).

from p in db.LookUps
0
 

Author Comment

by:martin05
ID: 35136021
Thanks guys for your help .

Kusala I had tried that before and couldnt really get it working.

Craig I think you are on the right track but unfortunately I am getting the error below

Error      1      The type or namespace name 'LookUp' could not be found (are you missing a using directive or an assembly reference?)      

Trying to learn this is doing my head in .....lol
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 11

Expert Comment

by:Kusala Wijayasena
ID: 35136110
This should work

foreach( object item in checkedListBox1.CheckedItems )
{
    MessageBox.Show(((ZORK.Controls.LookUp)item).Description);
}

Open in new window


-Kusala
0
 

Author Comment

by:martin05
ID: 35136241
Kusala , it must be something stupid I am doing as i am getting the same error.
Thanks for your help it is appreciated
0
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 35137866
You need to determine what the type of the variable disabilitylist is. It's possible you (or whoever put your EF model together) renamed the collection so the entity is not named LookUp.

You could use the following to try to determine the type of the object. Whatever the messagebox spits out is the type you need to cast item to.

This really is pretty simple debugging 101. Figure out the type of the object and then cast the object to that type so you can access its properties.
foreach( object item in checkedListBox1.CheckedItems )
{
    MessageBox.Show( item.GetType().Name );
}

Open in new window

0
 

Author Closing Comment

by:martin05
ID: 35138289
I was being a complete donkey , this solved it for me.

Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

19 Experts available now in Live!

Get 1:1 Help Now