Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
CheckedListBox - Get Index Of Item By Key
Message
De
16/12/2009 20:56:10
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01439400
Message ID:
01439508
Vues:
25
Well, since it's bound to a DataTable, just loop through the DataTable (or, more accurately, through the DataTable.DefaultView, but I'm having trouble coming up with the correct syntax at the moment for use with the DefaultView):
private int _GetIndexOfItemByKey(int Key)
{
    int RetVal = -1;

    DataTable dt = (DataTable)chlObjects.DataSource;
    for (DataRow row in dt.Rows)
    {
        if (Convert.ToInt32(row["DataObjectId"]) == Key)
        {
            RetVal = dt.Rows.IndexOf(row);
            break;
        }
    }

    return RetVal;
}
>No, I want to lop thru all items, checked and unchecked, searching for a key. When the key is found, return it's index position.
>
>
>
>>I'm assuming that you want to spin through the Items that have been checked by the user? Maybe not, but I'm not quite clear on what you're wanting to accomplish.
>>
>>The CheckedListBox.Items is simply a collection of objects.
>>The CheckedListBox.CheckedItems is also a collection of objects.
>>
>>So you'd use object in your foreach. And you can use Items.IndexOf() method to find the index.
>>
>>
>>
>>foreach (object item in this.MyCheckedListBox.CheckedItems)
>>{
>>        // I'll let you fill in the blanks (this should give you a nudge)
>>	int x = this.MyCheckedListBox.Items.IndexOf(item);
>>}
>>
>>
>>~~Bonnie
>>
>>
>>
>>
>>>I'm trying to return the index of an item in a CheckedListBox using the value. The list was bound using:
>>>
>>>
>>>private void _LoadLists()
>>>{
>>>    string ObjectsQuery = "SELECT DataObjectId, AccessTableId, ObjectName FROM DataObject ORDER BY ObjectName";
>>>    DataSet dsObjects = _ExecuteQuery(ObjectsQuery);
>>>
>>>    chlObjects.DataSource = dsObjects.Tables[0];
>>>    chlObjects.DisplayMember = "ObjectName";
>>>    chlObjects.ValueMember = "DataObjectId"; 
>>>}
>>>
>>>
>>>Then, I have this method to get the index, but I'm not sure how to complete it:
>>>
>>>
>>>private int _GetIndexOfItemByKey(int Key)
>>>{
>>>    int RetVal = -1;
>>>
>>>    foreach (what??? Row in chlObjects.Items)
>>>    {
>>>     
>>>    }
>>>    
>>>    return RetVal;
>>>    
>>>}
>>>
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform