vb.net datagridview 未更新到绑定数据源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14574441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
datagridview not updating to bound datasource
提问by ErocM
I am trying to get the datagridview to update when I update the datasource and I'm having no luck whatsoever.
当我更新数据源时,我试图让 datagridview 更新,但我没有任何运气。
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():
这是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.
数据库更新没有问题,但 datagridview 不会更新。
回答by WozzeC
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.
好的,我来试试看。当与 DGV 一起使用时,绑定源非常有用。然而,他们往往令人敬畏,因为他们不提供任何信息,为什么他们不工作。
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:
如您所见,您将 AllNews() 作为数据源。但是您向 _dgbNews 添加内容并期望 Allnews() 发生变化。Protip,它没有。如果您要将 DataSource 设置为:
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:
那么至少在列表更新时你应该期待一些变化。现在这就是你真正想念的。在按钮保存单击您将一个项目添加到列表中,如果您完成上述操作,现在就可以了。但是等等,数据源改变了,什么也没发生。这是因为您没有告诉 datagridview 进行更改。使 Bindingsource 告诉它连接到的所有内容进行更新。有了这个:
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).
这比 DGV.Refresh (现在可能工作)更好,因为您的 bindingsource 可以附加到其他东西(我知道它不是,但供将来参考)。
Try this and well see if it will go better.
试试这个,看看它是否会更好。
回答by Harry
Had similiar issue with DataGridView not displaying any data from my datasource, figured out that my binding class properties were regular int or string instead of being declared with get set.
有与 DataGridView 不显示来自我的数据源的任何数据的类似问题,发现我的绑定类属性是常规的 int 或 string,而不是使用 get set 声明。
changed from
从改变
int RedeemID;
int RedeemID;
to
到
public int RedeemID { get; set; }
公共 int RedeemID { 获取;放; }
回答by SHRI
TRY THIS:
尝试这个:
(AFTER EXECUTING THE UPDATE COMMAND FROM THE DATA ADAPTER)
(从数据适配器执行更新命令后)
GRIDVIEW_NAME.DATABIND()
回答by Hanna Khoury
Me.TableAdapter1.Fill(Me.DataSet1.Table1)
Me.TableAdapter1.Fill(Me.DataSet1.Table1)

