DataGridView Binding Help Please

paulthepaddy

Well-known member
Joined
Apr 9, 2011
Messages
222
Location
UK
Programming Experience
Beginner
Hi Guys, i dont know how to bind a datagridview to another datagridview depending on the row selected

The user enters the reg and it checks info already in the database

When their is info it gets all the info and updates the cars property's

Create Car.png

The two DataGridView at the bottom of the picture are the items i want to bind

i already have the first one binded using a binding source

VB.NET:
Dim OrderNumberBS As New BindingSource

Me.OrderNumberBS.DataSource = Me.Car.OrderNumber ' 
Me.dgv_OrderNumbers.DataSource = OrderNumberBS

Car.OrderNumber = Public Property OrderNumber As New List(Of OrderNumber)

Then the OrderNumber class has a property of

Public Property WorkDone As New List(Of WorkDone)

So how can i bind the WorkDone to the 2nd DataGridView depending on the row selected from the 1st datagridview when the WorkDone List is a 'ListOf(WorkDone)' within car.OrderNumber

I hope this makes sense, and i hope i have done the wright thing using a bindingsouce :S

Thanks Guys
 
Firstly, if that property is a List(Of OrderNumber) then how is OrderNumber an appropriate name? If the type of the property was OrderNumber then OrderNumber would be appropriate but if it's a List then it refers to multiple items so the name should be OrderNumbers. Just because we're programming doesn;t mean that we forget simple grammar.

Secondly, I would suggest adding your BindingSources in the designer rather than in code. I'd still bind them in code, but creating them in the designer makes life easier.

As for the question:

1. Handle the CurrentChanged event of the first BindingSource.
2. In the event handler, get the Current property of the first BindingSource and cast it as type OrderNumber.
3. Get the WorkDone collection of that OrderNumber and bind it to the second BindingSource.
4. Bind the second BindingSource to the second DataGridView.
 
Hi jmcilhinney,

Thanks for your reply, i was trying to get the 'OrderNumber' from the first dataGridView, that must be why i couldn't get it to work.

Just an idea, but i had thought about changing the 'List(Of OrderNumber)' to 'BindingList(Of OrderNumber)'.

I have to go to work, so il do as you said above, im pretty sure i understand what i need to do, but i would appreciate your opinion on the BindingList idea.

Thanks

Paul Longden
 
The BindingList is a good idea if you intend to make changes to the data in code while bound. That's because it can raise events that will prompt the bound control to update itself.
 
Hi jmcilhinney,

woohoo i had a good idea lol, i had planned for the user to be able to add ordernumbers to the list, the car will get all its information from the database and i want to be able to show the user what work and order numbers are already present on the car, was thinking of using diffrent colours to show old and new, so yea i think a bindinglist would be a good idea.

do i still need to use a Binding source for linking the two datagridviews?
 
The BindingList and BindingSource implement some of the same behaviour. They both implement the IBindingList interface. The BindingSource will allow you to do all you need to do with a basic List as backing while the BindingList will internalise some of that behaviour. I would tend to use a BindingSource regardless, because it provides navigation and access to the current item.
 
Back
Top