Question DataGridView doesn't show changes of its DataSource

sebastian.

Member
Joined
Sep 20, 2009
Messages
17
Programming Experience
1-3
Hello everybody!

My application looks like the following:

1. An XML file is read using LINQ and the result is an enumeration of objects of a specific type (i don't use anonymous types). The result is then converted with result.ToList() to a standard list and saved to a list-variable.

2. I added "Datasource" -> "Object" -> my specific class as a datasource to my project.

3. Then I drag-and-dropped this one to my windows form and a DataGridView was created.

4. Then I have a BindingSource and a BindingNavigator.

5.
The datasource of my BindingSource is my list-variable from above.
The datasource of my DataGridView is my BindingSource.
The bindingsource of my BindingNavigator is my BindingSource.

When I load my application, the data of the XML file is shown in the DataGridView.

6. When I delete a row using the delete-button of the BindingNavigator toolbar, it is deleted in my list-variable from above as well (which is my datasource of my bindingsource), but not in my XML file...that's okay!

-> everything works fine so far.

7. Now I have a button which is called "update dgv" and when i click it, the XML file is queried again using LINQ and the result is again saved in my list-variable. Just before the new query results are assigned to the list-variable, it contains all the data except the deleted and after the assignment it contains all the data like in the xml file. So now the deleted DGV items are again in my list-variable -> okay.

BUT they won't show up in my DataGridView although the list-variable is still my datasource of my BindingSource. :(


Can anybody see a problem in my steps described above?
Do I have to assign the list-variable as the datasource of my BindingSource again, because this is the only step I did "by hand / with extra code" when I load the windows form and not using the VisualStudio interface.

Thanks in advance for your help!

Greetings from Germany, Sebastian
 

sebastian.

Member
Joined
Sep 20, 2009
Messages
17
Programming Experience
1-3
I got a step further, but don't understand the logic behind it...

When I reassign the list-variable to the bs.datasource after the codelines of the "update dgv"-button, the changes are shown in my DataGridView.

But why do I have to reassign it? I think I'm writing the xml-data during the "update process" to exactly the same list-variable...not a new instance or something like that...?!

Hope someone can explain me what's going on or has a better idea than this reassignment ("bs.datasource = list-variable")!

Greetings, Sebastian
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,169
Location
Sydney, Australia
Programming Experience
10+
The List(Of T) doesn't raise the necessary events to notify a bound control that the data has changed and it should update. You will need to either use a BindingList(Of T) instead or else call the methods of the BindingSource that explicitly tell the bound control to update, e.g. ResetBindings.
 

sebastian.

Member
Joined
Sep 20, 2009
Messages
17
Programming Experience
1-3
Hi!

First of all, thanks for the explanation.
Unfortunately the bs.resetbingings(true/false) doesn't do anything?!
I put it right after the "update process". If I write bs.datasource = list-variable at the same position instead, it's working...

To use your second solution with the BindingList(Of T), I would need to convert the result of the LINQ query (IEnumerable(Of T)) to BindingList(Of T) and I don't know how?! :(

Would it be a "problem" to use the line of code: bs.datasource = list-variable?
Of course it would be nice, if the resetbinings or whatever would work...

Greetings, Sebastian
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,169
Location
Sydney, Australia
Programming Experience
10+
If you're calling ResetBindings and it's not working the the list you're editing is not the list you're binding. If you are calling ToList on a LINQ query then you're creating a List object, which you are then presumably binding. If you then change the underlying data and call ToList on the LINQ query again you are creating a completely different List, so your only options are to rebind the new List or else manually merge its contents into the old.
 

sebastian.

Member
Joined
Sep 20, 2009
Messages
17
Programming Experience
1-3
Hm...okay.
It's a bit different, but you're probably right.

I read the xml using LINQ, convert it to a list using ToList(of T), binding this list-variable/object, deleting a row using the datagridview and see the changes in my list-variable/object.

Then I read the unmodified xml file again using LINQ, convert it again and assign it again to the same list-variable. So I see by hovering over it, that it has my deleted rows again.

But these rows are still missing in my datagridview...so it seems it still has the old list-variable as a datasource although I assign the new query results to the same list-variable...

Am I right?

So I can do this "bs.datasource = list-variable" again and it shouldn't cause any problems, right? Probably my old list-variable is then deleted by the garbage-collector?!

Greetings, Sebastian
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,169
Location
Sydney, Australia
Programming Experience
10+
Then I read the unmodified xml file again using LINQ, convert it again and assign it again to the same list-variable. So I see by hovering over it, that it has my deleted rows again.
That's your problem right there. You're assuming that if you assign a new List to the original variable then you also changing DataSource of the BindingSource, but you're not. The DataSource of the BindingSource refers to a List object. It doesn't know anything about your List variable. All it knows about is the object that was assigned to it, which was the object referred to by the List variable at the time. The fact that you assign a new List object to the variable means nothing to the DataSource, which still refers to the old List object.
 

sebastian.

Member
Joined
Sep 20, 2009
Messages
17
Programming Experience
1-3
Yes, that's what I thought when I read your previous posts! Thanks for the explanation!

So my solutions would be:

1. somehow only change/update the content of the list object
2. do "bs.datasource = list-variable" again after an update. My old list-variable is then deleted by the garbage-collector and the rest will probably work?!

Am I right?

Thanks again!

Sebastian
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,169
Location
Sydney, Australia
Programming Experience
10+
Correct. Assuming that there are no other references to the original List object then it will be cleaned up by the GC.

By the way, it's a well-worn convention to use myThing to refer to a Thing in example code. Your use of "list-variable" had me confused to begin with because I though the dash was a subtraction operator. I'd suggest when you write example code that you actually make it real code, e.g.
Code:
bs.DataSource = listVariable
or:
Code:
bs.DataSource = myList
Clarity is good.
 

sebastian.

Member
Joined
Sep 20, 2009
Messages
17
Programming Experience
1-3
Alright, I'll use the CODE tags in future posts!

Thanks again for your explanations :).

Sebastian
 
Top Bottom