Solved

getting selected checkedlistbox text values c sharp/Linq

Posted on 2011-03-14
8
1,246 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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