Solved

getting selected checkedlistbox text values c sharp/Linq

Posted on 2011-03-14
8
1,314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

623 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