Solved

getting selected checkedlistbox text values c sharp/Linq

Posted on 2011-03-14
8
1,232 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Events in static methods 3 50
Programmatically signing Word macros 4 61
Icons and Colors for Terms 3 24
Runtime Exceptions when trying to submit data 28 36
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 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

16 Experts available now in Live!

Get 1:1 Help Now