Owner drawn listbox binding problem.

vis781

Well-known member
Joined
Aug 30, 2005
Messages
2,016
Location
Cambridge, UK
Programming Experience
5-10
Hi all, I have been playing around with an owner drawn listbox and due to my lack of knowledge on this subject i have run into a problem. I can draw all the items no problem and set the colors etc. However when i try to set the datasource property of the listbox i get an invalid cast exception.

'System.data.datarowview cannot be converted to string'

Now i know why this is happening as i'm using the draw string method to enter the items into the listbox, however i don't seem to be able to get around it. Is there something i missing here when trying to bind an owner drawn list box to a datatable?

thnks.
 
Ok, solved that problem. It wasn't the binding causing the problem it was when i tried to delete a row from the datatable, for some reason it doesn't draw the items properly. It seems to add items back in without totally redrawing. It works fine when i use the Datarowcollection.remove method but thats no good as i can't make changes to the datasource.

any ideas would be appreciated.
 
Ok, so here's how far i've got. I can now bind the listbox to the datasource and draw the listbox items as i
need to using the following code in the drawitem event..

VB.NET:
Private sub Listbox1_drawitem(byval sender as object, byval e as system.windows.forms.
drawitemeventargs) handles listbox1.drawitem
 
e.drawbackground
 
'normal stuff to handle the different colors etc...
 
' get a string object from the datasource of the displaymember at the row index..
 
dim strout as string = directcast(me.datasource,datatable).rows(e.index).item(me.displaymember)
.tostring
 
e.graphics.drawstring(strout,brush,font..etc)

Now here's the problem. When i try to delete a row from the underlying datatable the collection
goes a bit nuts when it redraws and i get an exception..

VB.NET:
Deleted row information cannot be accessed through the row.

I'm assuming that this is because the drawitem event is trying to get a hold of this row and put in
back in the listbox, obviously it doesn't know that it shouldn't be there. My question is why?
This is the last time i shall ask and i'll have to put this behind me or i'm going to end up chucking my
pc out the window. Thanks for any help?!
 
Back
Top