I've written an MDI app using grids. The grids bind to business objects that implement BindingList and ITypedList. The app works just fine except that I would like to know if there's a way to have the selected row be independent when two windows displaying the same backend data are open. For instance if I have Window1->Grid1->BusinessData and Window2->Grid2->BusinessData (i.e., two windows displaying the same set of data), when I click on a row on Grid1, the same row gets selected on Grid2. My intention is to be able to look at different records in the two windows even though it's the same backend data. I've looked through the .NET documentation on BindingList and nothing jumped out at me that would cause this behavior, but I might have missed something obvious.
Thanks in advance.
Hi,
This is determined by the BindingContext. Typically, the BindingContext of a control is the form it's on. So I would think if your grids are on two different forms, they would not stay synchronized. Maybe both of your grids are bound to a data source that is on the main MDI form? If that's the case, then this is very easy to fix. All you have to do is set the BindingContext of the grid to the BindingContext of it's parent form. You would do this in the Form_Load or maybe right before or after you set the grid's DataSource.
Thanks for the response. The data source is not the main MDI form, but is from a static class. In essence the static class holds all the business data, which is acquired as it's needed. The use of a static class is probably equivalent to having the data source on the main MDI form. Trying to set the BindingContext as you suggested had no impact.
Your statement about BindingContext gave me an idea, and that was to create a new BindingContext for the grid prior to binding the data to it. That seems to work. I did verify that if I open a record in Form2, edit it, and then save it, then my bakground update gets reflected on Form1 showing all the data. So, that seems to be the trick. I'll need to flush this out with the other forms that are showing other data from the business object.
Hope this isn't a "kludge" fix, but really the only way to do it given that I'm working with a static class. Any thoughts on that?
No, this isn't a kludge, this is the way the BindingManager works.
Creating a new BindingContext for each grid is fine, but personally, I would use the BindingContext of the child form. This way the grid on each form will be in synch with any other controls on that same form. Also, it saves the memory of creating an extra BindingContext. I'm not sure this is a big deal, though. :)
smercurio said:Well, as I indicated I had tried to set the BindingContext to that of the child form, but it didn't work.
Oh, sorry, I missed that part. Hm. I guess maybe all of the child MDI forms share the same BindingContext by default.
Well, as I indicated I had tried to set the BindingContext to that of the child form, but it didn't work. Afterwards I thought of just creating the new BindingContext.
I understand your comment regarding staying in sync with other controls on the same form. Happily, that's not an issue in this case since these forms are intended as "overview" forms, and if a user wants to edit a record they double-click on the row and a separate form comes up to allow them to edit. There aren't other bound controls on the forms. But, the sync issue is definitely something to keep in mind for the future.
Thanks for the tip in the right direction!