I am trying to get the datagridview to update when I update the datasource and I'm having no luck whatsoever.
Here is my binding:
Private _dgbNews As SortableBindingList(Of SalesMessageRecord)
Private Sub SalesMessageScreen_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'_dgbNews.RaiseListChangedEvents = True
_dgbNews = AllNews()
BindingSource1.DataSource = AllNews()
DataGridView1.DataSource = BindingSource1
MassageDemRows()
End Sub
this is AllNews()
:
Public Function AllNews() As SortableBindingList(Of SalesMessageRecord)
Dim sm = New SortableBindingList(Of SalesMessageRecord)
Dim allnewsitems = News.GetAllNewsItems(Configuration.CompanyID).ToList()
For Each allnewz As News In allnewsitems
Dim smr = New SalesMessageRecord
smr.Body = allnewz.NewsBody
smr.CorporationId = CType(allnewz.CorporationId, Guid)
smr.Expiration = allnewz.Expiration
smr.IsActive = allnewz.IsActive
smr.NewsId = allnewz.NewsId
smr.Title = allnewz.NewsTitle
smr.SortOrder = allnewz.OrderNumber
smr.TokenId = allnewz.TokenId
smr.IsNew = False
sm.Add(smr)
Next
Return sm
End Function
And this is where I'm trying to update it:
Private Sub button_Save_Click(sender As System.Object, e As System.EventArgs) Handles button_Save.Click
If _currentRow < 0 Then
Return
End If
_dgbNews(_currentRow).Expiration = datetimepicker_ExpirationDate.Value
_dgbNews(_currentRow).SortOrder = CInt(numericupdown_SortNumber.Value)
_dgbNews(_currentRow).IsActive = checkbox_Active.Checked
_dgbNews(_currentRow).Body = richtextbox_Body.Text
_dgbNews(_currentRow).Title = textbox_Title.Text
DataGridView1.Refresh()
News.UpdateNewsRecord(_dgbNews(_currentRow).NewsId,
_dgbNews(_currentRow).Expiration,
_dgbNews(_currentRow).SortOrder,
_dgbNews(_currentRow).IsActive,
_dgbNews(_currentRow).Body,
_dgbNews(_currentRow).Title)
End Sub
The database is updating without issue but the datagridview won't update.
Right, I'll take a crack at this. Bindingsources are really useful when used together with a DGV. However, they tend to be aweful in the way that they give no information what so ever why they aren't working.
First of I would say that your binding source has the wrong Datasource.
_dgbNews = AllNews()
BindingSource1.DataSource = AllNews()
DataGridView1.DataSource = BindingSource1
As you can se you have AllNews() as Datasource. But you add stuff to _dgbNews and expect Allnews() to change. Protip, it doesn't. If you were to set the DataSource to this:
BindingSource1.DataSource = _dgbNews
Then atleast you should expect some change when the list is updated. Now this is what you actually do miss. In button save click you add an item to the list, this is now fine if you done the above. But wait, the datasource changed and nothing happened. This is because you don't tell the datagridview to change. Make the Bindingsource tell everything it's connected to to update. With this:
BindingSource1.ResetBindings(True)
This is better than DGV.Refresh (which might work now) since your bindingsource could be attached to other things (I know it isn't, but for future reference).
Try this and well see if it will go better.