vb.net 如何向绑定源添加新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38906588/
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
How do I add a new row to a binding source
提问by Ken
I am trying to programmatically add a new row to a binding source . I know calling the bsSource.AddNew() adds a new row which I cast as a DataRowView and I set my values. My problem is this - the DataRowView.Row shows a RowState of detached. I do not want it to be detached ; I believe it should show added - I also do NOT want it to commit the change to the database (There is a very valid reason for this). I want to pick the time for that later. My code is as follows:
我正在尝试以编程方式向绑定源添加新行。我知道调用 bsSource.AddNew() 添加一个新行,我将其转换为 DataRowView 并设置我的值。我的问题是 - DataRowView.Row 显示 RowState 为 detached。我不希望它被分离;我相信它应该显示已添加 - 我也不希望它提交对数据库的更改(有一个非常有效的理由)。我想稍后选择时间。我的代码如下:
private Sub AddToRelationSource(binID As Integer, gradeID As Integer, IsChecked As Boolean)
Dim drv As DataRowView = DirectCast(bsBinGrades.AddNew(), DataRowView)
drv.Row("IsSelected") = IsChecked
drv.Row("BinID") = binID
drv.Row("GradeID") = gradeID
' I tried drv.EmdEdit(0 drv.Row.EndEdit() - Row State still shows detached
End Sub
回答by Ken
The BindingSource AddNew method does not actually add a new record to the underlying datasource , it simply adds it to the bindingsource as a detached item. When using the datatabel as a datasource I needed to get the datatable and use the AddRow() method - this properly set the value in my bindingsource to added so that when the changes would be committed to the database on bindingsource.Update() method.
BindingSource AddNew 方法实际上并未向基础数据源添加新记录,它只是将其作为分离项添加到 bindingsource 中。当使用数据表作为数据源时,我需要获取数据表并使用 AddRow() 方法 - 这将我的 bindingsource 中的值正确设置为 added 以便何时将更改提交到 bindingsource.Update() 方法上的数据库。
The code I used:
我使用的代码:
Dim drv As DataRowView = DirectCast(bsData.AddNew(), DataRowView)
drv.BeginEdit()
drv.Row.BeginEdit()
drv.Row("IsSelected") = IsChecked
drv.Row.EndEdit()
drv.DataView.Table.Rows.Add(drv.Row)
The last line is what actually added the item to the datasource - I misunderstood BindingSource.AddNew() .
最后一行是将项目实际添加到数据源的内容 - 我误解了 BindingSource.AddNew() 。
回答by Karen Payne
The following may be in the right direction. First I used a few language extension methods e.g.
以下可能是正确的方向。首先我使用了一些语言扩展方法,例如
Public Module BindingSourceExtensions
<Runtime.CompilerServices.Extension()>
Public Function DataTable(ByVal sender As BindingSource) As DataTable
Return CType(sender.DataSource, DataTable)
End Function
<Runtime.CompilerServices.Extension()>
Public Sub AddCustomer(ByVal sender As BindingSource, ByVal FirstName As String, ByVal LastName As String)
sender.DataTable.Rows.Add(New Object() {Nothing, FirstName, LastName})
End Sub
<Runtime.CompilerServices.Extension()>
Public Function DetachedTable(ByVal sender As BindingSource) As DataTable
Return CType(sender.DataSource, DataTable).GetChanges(DataRowState.Detached)
End Function
<Runtime.CompilerServices.Extension()>
Public Function AddedTable(ByVal sender As BindingSource) As DataTable
Return CType(sender.DataSource, DataTable).GetChanges(DataRowState.Added)
End Function
End Module
Now load ID, FirstName and LastName into a DataTable, Datatable becomes the DataSource of a BindingSource which is the BindingSource for a BindingNavigator and are wired up to a DataGridView.
现在将 ID、FirstName 和 LastName 加载到 DataTable 中,Datatable 成为 BindingSource 的 DataSource,它是 BindingNavigator 的 BindingSource 并连接到 DataGridView。
Keeping things simple I mocked up data, has no assertions e.g. make sure we have valid first and last name, instead concentrate on the methods.
保持简单我模拟了数据,没有断言,例如确保我们有有效的名字和姓氏,而是专注于方法。
First use a extension method to add a row to the underlying DataTable of the BindingSource.
首先使用扩展方法向 BindingSource 的底层 DataTable 添加一行。
bsCustomers.AddCustomer("Karen", "Payne")
Now check to see if there are detached or added rows
现在检查是否有分离或添加的行
Dim detachedTable As DataTable = bsCustomers.DetachedTable
If detachedTable IsNot Nothing Then
Console.WriteLine("Has detached")
Else
Console.WriteLine("No detached")
End If
Dim AddedTable As DataTable = bsCustomers.AddedTable
If AddedTable IsNot Nothing Then
Console.WriteLine("Has added")
Else
Console.WriteLine("None added")
End If
Since we are not talking to the database table, the primary key is not updated as expected and since you don't want to update the database table this is fine. Of course there is a method to get the primary key for newly added records if you desire later in your project.
由于我们不是在与数据库表交谈,因此主键没有按预期更新,并且由于您不想更新数据库表,这很好。当然,如果您希望稍后在项目中使用,可以使用一种方法来获取新添加记录的主键。
Addition
添加
Private Sub BindingSource1_AddingNew(ByVal sender As System.Object, ByVal e As System.ComponentModel.AddingNewEventArgs) Handles BindingSource1.AddingNew
Dim drv As DataRowView = DirectCast(BindingSource1.List, DataView).AddNew()
drv.Row.Item(0) = "some value"
e.NewObject = drv
' move to new record
'BindingSource1.MoveLast()
End Sub

