|
-
Dec 1st, 2015, 01:08 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Cant bind correctly to DataGridView
Hiya peeps!
I have a DataGridView added to my windows form application form. Some of the columns are DataGridViewComboBoxColumn columns.
I would like to populate the column combo-boxes from members of objects that are in a list. I previously had this working just fine in a test implementation by setting the data source of the column to the list and setting the DataPropertyName of the column to a string of the name of the relevant member.
I've changed this implementation now to use a list of a slightly more complex type of object, the list is now a member of another object. However now the ComboBox is populated by the name of the list objects class and upon selection gives an error dialog for:
"System.ArgumentException: DataGridViewComboBoxCell value is not valid."
Which figures because the name of the class is obviously not what is listed in the data members string.
Eg. A class is called baritem, objects of this type are listed on a BindingList(Of baritem) named bar, which is a member within the foo class:
vb Code:
Public Class baritem Public Property peanuts As String End Class Public Class foo Public Property bar As BindingList(Of baritem) End Class
I declare a foo type object and add some data for a single item on the list:
vb Code:
Dim foobar As foo = New foo() With { _ .bar = New BindingList(Of baritem) From { _ New baritem() With { _ .peanuts = New String("dodgy") _ } _ } _ }
Then in the forms OnLoad event handler I setup the data binding to one of the columns in the DataGridView. The grid has a column named Description that should match up to the peanuts member of the listed baritem object:
VB Code:
ProfileDataGridView.AutoGenerateColumns = False Dim gdvcbc As DataGridViewComboBoxColumn = ProfileDataGridView.Columns("Description") gdvcbc.DataSource = foobar.bar gdvcbc.DataPropertyName = "peanuts"
This all runs without exception (It's in a Try Catch) and the form displays. Clicking on the ComboBox dropdown displays "ProfileMaker.ProfileMaker+baritem" instead of the expected "dodgy". On selecting this item and confirming the selection the error as outlined appears.
I tried adding Dim baritemlist As new BindingList(Of baritem) = foobar.bar and binding to that reference instead but with no change in the result.
The error is particularly annoying because it seems to be saying: "Yo, we heard you like strings in your ComboBoxes so we put a string in the ComboBox that's not in the list you provided and now we're gonna complain that it's not in the list you provided. Go fixthat, code monkey!".
A) How the flip is it even getting the class name as a string?!?
B) How can I fix it?
C) How many more times can I think I've finally gotten the hang of this VB lark to find another stinking pile of badly-documented illogicaly-behavioured dog's doo of a control that sets me back an entire day without putting my fist through the screen?
Thanks 
-
Dec 1st, 2015, 02:36 PM
#2
Re: Cant bind correctly to DataGridView
set the propertyname first then the datasource. Might fix it, might not. I know when binding to a regular combobox, you set the member properties first and then the datasource. Has something to do with the way the control is rendered. At the time you set hte data source, it doesn't know what property to use as the display... so it uses the only thing it has - the .ToString ... which isn't implemented, so it reverts to the default version of that (which comes from Object from which all things come from) which in turn simply returns the type. If you want something else you need to override the ToString method and have it return what you want to return.
But before messing with ToString, try the other thing first.
-tg
-
Dec 2nd, 2015, 05:14 AM
#3
Thread Starter
Hyperactive Member
Re: Cant bind correctly to DataGridView
Thanks Techgnome. while your answer didn't directly solve the issue it (and a nights sleep) set my mind on the right path.
I'd been mixing up the bindings! Basically, at the moment the DataGridView rows are bound to the same list that the DataGridViewComboboxColumn ComboBox controls are bound to.
i.e. the same list populates the comboboxes on each row and the overall rows themselves.
This meant my head was mixing up which data binding code was serving which binding! The issue was fixed by adding a .DisplayMember set so the ComboBox knows which member to bind to. The .DataPropertyName member setting I had previously thought was controlling this was actually controlling which object member bound to that column for the rows binding.
I think.
Seems to be working now anyway 
My binding code is now as follows:
vb Code:
Dim gdvcbc As DataGridViewComboBoxColumn
'Populate DataGridViewComboboxColumn ComboBox
gdvcbc = ProfileDataGridView.Columns("Description")
gdvcbc.DisplayMember = "Description" 'Added this
gdvcbc.DataSource = TestData.Rows
'Populate DataGridView controls
ProfileDataGridView.AutoGenerateColumns = False
gdvcbc.DataPropertyName = "Description" 'Remembered this is for the row binding, NOT the column binding
ProfileDataGridView.DataSource = TestData.Rows
TY, W99
Thanks 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|