ListBox Datatable changes reflected immediately, but out of sync??

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm confused.

I have a listbox. The DataSource is a DataTable based on a SQL Select
Statement that includes a where clause. For example, "Select ID, FirstName,
LastName From Employees where DepartmentID = 5"

I have three controls on the same form that are bound to this datatable. So
far so good. I click through the list in the listbox, and the controls
reflect the current selected item in the listbox. (Nice!)

Ok, here's where it gets weird. If I change the departmentID to 6 for one
of the rows, that employee disappears from the list box. At first I thought
this was really cool, but when clicking on items in the list now, the
databinding is out of sync.

Let's say I changed the first employee in the list to DepartmentID =6. It
disappears from the list. If I click on the second item in the list, I see
field values for the first item. If I click on the first item on the list, I
see field values for the changed employee that is no longer in the list.

What is happening, and how do I fix it?
 
As a separate, but related question, why is it that clicking on an item in
the listbox moves the position of the datatable? I have not set up any
connections to a BindingManagerBase or anything like that. It's a nice
convenience (maybe) but I need to understand how this is working so I can
control it better.

Thanks in advance...
 
Hi,

Zorpiedoman said:
I'm confused.

I have a listbox. The DataSource is a DataTable based on a SQL Select
Statement that includes a where clause. For example, "Select ID,
FirstName,
LastName From Employees where DepartmentID = 5"

I have three controls on the same form that are bound to this datatable.
So
far so good. I click through the list in the listbox, and the controls
reflect the current selected item in the listbox. (Nice!)

That shouldn't happen automatically. Normally you have to use a DataView
and set the RowFilter to "DepartmentID = 5" for this to happen.
Ok, here's where it gets weird. If I change the departmentID to 6 for one
of the rows, that employee disappears from the list box. At first I
thought
this was really cool, but when clicking on items in the list now, the
databinding is out of sync.

Let's say I changed the first employee in the list to DepartmentID =6. It
disappears from the list. If I click on the second item in the list, I
see
field values for the first item. If I click on the first item on the
list, I
see field values for the changed employee that is no longer in the list.

What is happening, and how do I fix it?

Are you by any change handling either SelectedIndexChanged or any of the
other Selected Changed events ?

Be carefull that you do not cause an exception inside, because they are
masked (hidden) and cause problems like you are describing.

So everything inside SelectedIndexChanged should be put inside a try/catch
and inside catch display the error, so you can see if anything goes wrong.

hth,
greetings
 
Hi,
Zorpiedoman said:
As a separate, but related question, why is it that clicking on an item in
the listbox moves the position of the datatable? I have not set up any
connections to a BindingManagerBase or anything like that. It's a nice
convenience (maybe) but I need to understand how this is working so I can
control it better.

Because the Control are bound to the same DataSource ( DataTable in this
case ) and they share the same BindingContext(default) so they will use the
same CurrencyManager (keeps position) and navigate together.

Hth,
Greetings
 
"> That shouldn't happen automatically. Normally you have to use a DataView
and set the RowFilter to "DepartmentID = 5" for this to happen."

EXACTLY... This should not happen. Yet I am not setting up any filters, and
it DOES happen... I thought that to be really 'interesting.'


Are you by any change handling either SelectedIndexChanged or any of the
other Selected Changed events ?

Be carefull that you do not cause an exception inside, because they are
masked (hidden) and cause problems like you are describing.

So everything inside SelectedIndexChanged should be put inside a try/catch
and inside catch display the error, so you can see if anything goes wrong.

I am, but there are no errors occuring.
 
I'll keep responding to my own questions, and maybe I'll figure it out
myself...

Anyway, in looking at the call stack, changing the selectedindex of the
listbox is calling the Set_Position method of the form's Bidning context.

I did nothing to set this up, it seems automatic.

Where is someone from MS to help with this question???

-zorpid
 
Hi,

Zorpiedoman said:
"> That shouldn't happen automatically. Normally you have to use a
DataView

EXACTLY... This should not happen. Yet I am not setting up any filters,
and
it DOES happen... I thought that to be really 'interesting.'




I am, but there are no errors occuring.

Are you sure? Seems a little coincidental that you are handling
SelectedIndexChanged and seeing this problem. Have you put everything
inside a try/catch ? Have you tried completely removing (commenting out)
the SelectedIndexChanged handlers to see if it works ?

Hth,
Greetings
 
Hi,

Zorpiedoman said:
I'll keep responding to my own questions, and maybe I'll figure it out
myself...

Anyway, in looking at the call stack, changing the selectedindex of the
listbox is calling the Set_Position method of the form's Bidning context.

I did nothing to set this up, it seems automatic.

Nothing special about that. If you bind a ListBox to a DataTable there will
be a CurrencyManager created in the BindingContext of the Form. ( The
BindingContext of the Form is by default shared by all child controls. )

The CurrencyManager maintance the current position so it's normal it's
linked to the SelectedIndex of the ListBox.

hth,
Greetings
 
Back
Top