Re: ComboBox ItemData?

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Ginny Caughey [MVP] (ginny.caughey.online_at_wasteworks.com)
Date: 10/20/04


Date: Wed, 20 Oct 2004 07:53:15 -0400

Peter,

The business objects I use never get deleted, so I never ran into the
situation you describe. If I had though, I think I would have used the same
solution that you did. With data binding particularly, and not just .Net
data binding but every kind I've ever used, there always seems to be
somewhat of a trial-and-error phase until I see what it really does.

-- 
Ginny Caughey
.Net Compact Framework MVP
"PeterB" <peter@data.se> wrote in message 
news:%23AxPFmntEHA.3564@tk2msftngp13.phx.gbl...
> Ginny, if you use the ArrayList of objects method, do you have any trouble 
> with removing items from the list?
>
> I Add a bunch of objects to my list and set the DataSource of the combobox 
> to the list. All items are displayed (according to the DisplayMember of 
> the combo). If I select the _last_ item in the list, and remove it. The 
> combobox will change to SelectedIndex -1.
>
> private void btnDelete_Click(object sender, System.EventArgs e)
> {
>    if( cmbBoxItems.SelectedIndex != -1 )
>    {
>         lst.Remove( (MyClass)cmbBoxItems.SelectedItem );
>         ResetDataBinding();
>    }
> }
>
> After this I need to reset the DataSource since my ArrayList doesn't 
> implement IBindingList and doesn't propagate the changes automatically to 
> the combobox. I set DataSource to null and then reset it to the list 
> again.
>
> The combo is updated and now contains 1 object less than before. If I now 
> select the last item... CRASH! ArgumentOutOfRangeException is thrown.
>
> If I change the delete handler to the following, I don't get an exception 
> however:
>
> private void btnDelete_Click(object sender, System.EventArgs e)
> {
>    int indx = cmbBoxItems.SelectedIndex;
>    if( indx != -1 )
>    {
>        if( indx == cmbBoxItems.Items.Count - 1 && cmbBoxItems.Items.Count 
>  > 1 ) // Last obj
>        {
>            MyClass mc = (MyClass)cmbBoxItems.SelectedItem;
>            cmbBoxItems.SelectedIndex--;
>            lst.Remove( mc );
>        }
>        else
>            lst.Remove( (MyClass)cmbBoxItems.SelectedItem );
>
>        ResetDataBinding();
>    }
> }
>
> Is it neccessary to perform the actions showed in the second code snippet. 
> I prevent the SelectedIndex = -1, but that's about the only difference... 
> I guess this is a bug somewhere in either ComboBox or ArrayList...
>
> regards,
>
> Peter
>
>
>
> "Ginny Caughey [MVP]" <ginny.caughey.online@wasteworks.com> skrev i 
> meddelandet news:%23lL$zgetEHA.1368@TK2MSFTNGP15.phx.gbl...
>> Tim,
>>
>> I use the ArrayList of objects approach described by Alex. The objects I 
>> use like this all have a property called DataObject that looks like this:
>>
>> public Customer DataObject
>> { get{return tnis;}}
>>
>> so I can just set the ValueMember of the combo box to DataObject and get 
>> back the data object itself.
>>
>> -- 
>> Ginny Caughey
>> .Net Compact Framework MVP
>>
>>
>>
>> "Tim Johnson" <tjohnson@high-point.com> wrote in message 
>> news:eU55kFatEHA.1404@TK2MSFTNGP11.phx.gbl...
>>> I'm coming from MFC-land where a combobox had an ItemDataPtr property,
>>> whereby you could stash away a ptr to some arbitrary object.  Then when 
>>> the
>>> user selected an item, you could fetch the object back based on the 
>>> selected
>>> index.  Is there anything comparable in C#?  The only samples I've seen 
>>> show
>>> them managing their own separate arraylist corresponding to the items in 
>>> the
>>> combobox - yechh!
>>>
>>> -- 
>>> Tim Johnson
>>> High Point Software
>>> www.high-point.com
>>> (503) 312-8625
>>>
>>>
>>
>>
>
> 


Relevant Pages

  • Re: ComboBox ItemData?
    ... Ginny, if you use the ArrayList of objects method, do you have any trouble ... I Add a bunch of objects to my list and set the DataSource of the combobox ... prevent the SelectedIndex = -1, but that's about the only difference... ... > use like this all have a property called DataObject that looks like this: ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: How to databind a combo that has its items entered through the IDE?
    ... Afaik SelectedIndex is not bindable. ... Creating a datasource is not that hard either :-) ... miha at rthand com ... Use a datatable with two columns for combobox datasource: ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: set selected index of a ComboBox
    ... //I also tried an ArrayList with Strings as DataSource ... You should use SelectedIndex property! ... runnable code sample in the ComboBox class overview. ... Dim index As Integer ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Bindinglist und DataGridView
    ... Jetzt binde ich die BL_Kategorien an eine ComboBox ... Und an SelectedValue wird ein weiterer CurrencyManager angehängt, ... Ein DataGridist bindet ebenso an eine eigene DataSource. ... Nun sollte nun mit der DataGridViewComboBoxColumn gleiches wie bei der ...
    (microsoft.public.de.german.entwickler.dotnet.datenbank)
  • Re: Using a ComboBox selection to fill in other fields in form
    ... > The Name of the Combo Box is 'Item1Desc' It's DataSource is also ... > The name of the TextBox to be filled in is "Item1Rate". ... that is what the ComboBox and the field in the table used ...
    (microsoft.public.access.forms)