Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

getting selected checkedlistbox text values c sharp/Linq

Posted on 2011-03-14
8
Medium Priority
?
1,341 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
Technology Partners: 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!

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

670 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