绑定导航器不会在第一次单击时添加记录,但会在后续单击时添加 [英] Binding navigator not adding record on first click, but does on subsequent clicks

查看:94
本文介绍了绑定导航器不会在第一次单击时添加记录,但会在后续单击时添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当简单的小表单,带有一个标签容器,用于向SQL数据库添加一些信息。在其中一个标签页上,我有几个文本框和几个组合框。在这个页面的顶部是一个绑定导航器。

有一件事使得这有点不典型的是,它不是绑定到我的数据库中的表,而是(部分)绑定到数据表我在表单中的代码中创建。

除了一个必须解决的恼人问题外,事情大多数情况下都是如此。

当我点击导航工具条中的加号时第一次不向我的数据表添加行。它将在随后的点击中进行,但随后工具条上的计数器总是再显示数据表中的实际内容。我只是不知道如何解决这个问题。谷歌搜索绑定导航器的示例绑定到表只有代码无济于事。所有示例都绑定到数据库中的表。如果有帮助,我可以附上我的表格代码(你如何在这里附上文件?)

请帮忙!!



好​​的,我我看看我是否可以在这里粘贴表单代码...



Imports System.Windows.Forms

Imports System.Data

导入System.Data.SqlClient



公共类CableNewForm



'定义我们的临时数据表的对象

Dim Cores_DataTable As New DataTable

Dim Cores_DataRow As DataRow

Dim Cores_Binding as New BindingSource



Public Sub New()

'设计师需要此调用。

InitializeComponent()

'在InitializeComponent()调用之后添加任何初始化。

'第一个标签页属性

尝试

使用连接为新SqlClient.SqlConnection (databaseOps.connectionString)

connection.Open()

Dim da As New SqlDataAdapter(select Manufacturer from Manufacturer,connection)



'组合框被绑定到SQL数据库中的各个表

Dim manufacturer_dt As New DataTable

da.Fill(manufacturer_dt)

如果manufacturer_dt.Rows.Count> ; 0然后

Me.ComboBoxManufacturer.DataSource = manufacturer_dt

Me.ComboBoxManufacturer.DisplayMember =制造商

Me.ComboBoxManufacturer.ValueMember =制造商

Me.ComboBoxManufacturer.SelectedIndex = 0

结束如果



da.SelectCommand.CommandText =select libraryname from library

Dim library_dt As New DataTable

da.Fill(library_dt)

if library_dt.Rows.Count> 0然后

Me.ComboBoxLibrary.DataSource = library_dt

Me.ComboBoxLibrary.DisplayMember =libraryname

Me.ComboBoxLibrary.ValueMember =libraryname

Me.ComboBoxLibrary.SelectedIndex = 0

结束如果



da.SelectCommand.CommandText =select供应商供应商

Dim supplier_dt As New DataTable

da.Fill(supplier_dt)

如果supplier_dt.Rows.Count> 0然后

Me.ComboBoxSupplier.DataSource = supplier_dt

Me.ComboBoxSupplier.DisplayMember =供应商

Me.ComboBoxSupplier.ValueMember =供应商

Me.ComboBoxSupplier.SelectedIndex = 0

结束如果



da.SelectCommand.CommandText =select cabletypeName来自cabletypes

Dim cabletype_dt As New DataTable

da.Fill(cabletype_dt)

if cabletype_dt.Rows.Count> 0然后

Me.ComboBoxCableType.DataSource = cabletype_dt

Me.ComboBoxCableType.DisplayMember =cabletypeName

Me.ComboBoxCableType.ValueMember =cabletypeName

Me.ComboBoxCableType.SelectedIndex = 0

结束如果



da.SelectCommand.CommandText =select颜色的颜色代码

Dim cablecolor_dt As New DataTable

da.Fill(cablecolor_dt)

if cablecolor_dt.Rows.Count> 0然后

Me.ComboBoxCableColor.DataSource = cablecolor_dt

Me.ComboBoxCableColor.DisplayMember =colorcode

Me.ComboBoxCableColor.ValueMember =colorcode

Me.ComboBoxCableColor.SelectedIndex = 0

结束如果

结束使用



'在属性标签页上设置一些默认值

InsertedUserTextBox.Text = currentUser

InsertedDateTimePicker.Text = DateTime.Now.ToString(yyyy / MM / dd)

UpdatedDateTimePicker.Text = DateTime.Now.ToString(yyyy / MM / dd)

NumberOfCoresTextBox.Text =0

HasShieldCheckBox .Checked = False

HasGroundCheckBox.Checked = False

CableDefIdTextBox.Text = - 1



Catch sqex As SqlClient.SqlException

MsgBox(Sql Exception:& sqex.Message)

Catch ex As DataException

MsgBox(ex.Message)

结束尝试



'第二个标签页核心

尝试

使用连接为新SqlClient.SqlConnection(databaseOps.connectionString)

connection.Open()



'再次,组合框绑定到数据库中的表

使用wiretype_da作为新的SqlDataAdapter(select来自wiretypes的wiretypename,连接)

Dim wiretype_dt As New DataTable

wiretype_da.Fill(wiretype_dt)

如果是wiretype_dt.Rows.Count> 0然后

Me.ComboBoxWireType.DataSource = wiretype_dt

Me.ComboBoxWireType.DisplayMember =wiretypename

Me.ComboBoxWireType.ValueMember =wiretypename

Me.ComboBoxWireType.SelectedIndex = 0

结束如果

结束使用



使用wiregauge_da作为新的SqlDataAdapter(从wiregauges选择wiregaugecode,连接)

Dim wiregauge_dt As New DataTable

wiregauge_da.Fill(wiregauge_dt)

如果wiregauge_dt.Rows.Count> 0然后

Me.ComboBoxWireGauge.DataSource = wiregauge_dt

Me.ComboBoxWireGauge.DisplayMember =wiregaugecode

Me.ComboBoxWireGauge.ValueMember =wiregaugecode

Me.ComboBoxWireGauge.SelectedIndex = 0

结束如果

结束使用



使用wirecolor_da作为新的SqlDataAdapter(从颜色中选择颜色代码,连接)

Dim wirecolor_dt As New DataTable

wirecolor_da.Fill(wirecolor_dt)

如果wirecolor_dt.Rows.Count> 0然后

Me.ComboBoxWireColor.DataSource = wirecolor_dt

Me.ComboBoxWireColor.DisplayMember =colorcode

Me.ComboBoxWireColor.ValueMember =colorcode

Me.ComboBoxWireColor.SelectedIndex = 0

结束如果

结束使用

结束使用



'在核心标签页上设置一些默认值

CableDefIdTextBox1.Text = - 1



Catch sqex为SqlClient.SqlException

MsgBox(Sql Exception:& sqex.Message)

Catch ex As DataException

MsgBox(ex.Message)

结束尝试



'创建一个包含电缆核心的临时数据表。

'单击确定按钮时,这些按钮将被写入数据库ase

Cores_DataTable.Columns.Add(CableWireDefId,Type.GetType(System.Int32))

Cores_DataTable.Columns.Add(WireType,Type。 GetType(System.String))

Cores_DataTable.Columns.Add(CableDefId,Type.GetType(System.Int32))

Cores_DataTable.Columns。添加(WireGauge,Type.GetType(System.String))

Cores_DataTable.Columns.Add(WireGroup,Type.GetType(System.String))

Cores_DataTable.Columns.Add(WireColor,Type.GetType(System.String))

Cores_DataTable.Columns.Add(Voltage,Type.GetType(System。 Double))

Cores_DataTable.Columns.Add(Attenuation,Type.GetType(System.Double))

Cores_DataTable.Columns.Add(Resistance ,Type.GetType(System.Double))

'Cores_DataTable.Constraints.Add(me,Cores_DataTable.Columns.Item(Cablewiredefid),primaryKey:= True)



'绑定数据表到导航器对象

Cores_Binding.DataSource = Cores_DataTable

Me.BindingNavigator1.BindingSource = Cores_Binding



'绑定文本字段

CableWireDefIdTextBox.DataBindings.Add(Text,Cores_Binding,CableWireDefId)

WireGroupTextBox.DataBindings.Add(Text,Cores_Binding,WireGroup )

VoltageTextBox.DataBindings.Add(Text,Cores_Binding,Voltage)

AttenuationTextBox.DataBindings.Add(Text,Cores_Binding,Attenuation)

ResistanceTextBox.DataBindings.Add(Text,Cores_Binding,Resistance)

End Sub



Private Sub KillButton_Click(发送者为对象,e为EventArgs)处理KillButton.Click

Me.Close()

结束子



Private Sub OkButton_Click(发送者作为对象,e作为EventArgs)处理OkButton.Click

'他新的电缆将被添加到数据库中,其核心将来自我们在此表单中定义的

'临时数据表。

Me.Close()

End Sub



Private Sub TabControl1_SelectedIndexChanged(sender As Object,e As EventArgs)Handles TabControl1.SelectedIndexChanged

Dim cabletype_index As整数= 0



'更新属性表上的核心数

NumberOfCoresTextBox.Text = Cores_DataTable.Rows.Count

如果Cores_DataTable.Rows.Count> 1然后

cabletype_index = Me.ComboBoxCableType.FindString(Cable)

Me.ComboBoxCableType.SelectedIndex = cabletype_index

结束如果



'如果其中一个核心是盾牌或地面,请选中属性选项卡中的方框

For Each row As DataRow In Cores_DataTable.Rows

如果row.Item(WireType)=Shield那么

HasShieldCheckBox.Checked = True

cabletype_index = Me.ComboBoxCableType.FindString( 电缆,屏蔽)

Me.ComboBoxCableType.SelectedIndex = cabletype_index

结束如果

如果row.Item(WireType)=地面然后

HasGroundCheckBox.Checked = True

结束如果

下一页

结束次级



'绑定导航器覆盖----------------------------------- ------ -------------------------------------------------- --------



Private Sub fixComboBoxes()

'按ID获取电线规格

Dim type_index As Integer = 0

'临时变量,我可以在调试时查看,以后再删除。

Dim temp As String = String.Empty

Dim temp2 As Object



For Each Row As DataRow In Cores_DataTable.Rows

temp = row.Item(CableWireDefId ).ToString()

如果CableWireDefIdTextBox.Text = temp那么

temp2 = row.Item(WireType)

type_index = ComboBoxWireType .FindString(row.Item(WireType))'这可以为null!修理它!

ComboBoxWireType.SelectedIndex = type_index

temp2 = row.Item(WireGauge)

type_index = ComboBoxWireGauge.FindString(row.Item( WireGauge))

ComboBoxWireGauge.SelectedIndex = type_index

temp2 = row.Item(WireColor)

type_index = ComboBoxWireColor.FindString(row .Item(WireColor))

ComboBoxWireColor.SelectedIndex = type_index

结束如果

下一页

End Sub



Private Sub BindingNavigatorAddNewItem_Click(sender As Object,e As EventArgs)处理BindingNavigatorAddNewItem.Click

Dim rownum As Int16 =(Convert.ToInt16( BindingNavigator1.PositionItem.Text) - 1)

'出于某种原因,这不是在第一次点击时添加到cores_dt,

'但它会增加计数器在...上navigator strip to 1

Cores_Binding.AddNew()

'如果第一次点击没有,这将添加一行给cores_dt,但随后它会增加

'计数器再次表示2,即使数据表中只有1行

如果Cores_DataTable.Rows.Count = 0那么

'Cores_Binding.AddNew( )

结束如果

'需要手动添加所选择的组合框索引,因为它们没有绑定。

如果Cores_DataTable.Rows.Count> ; 0然后

Cores_DataTable.Rows(rownum).Item(CableWireDefId)= Convert.ToInt32(BindingNavigator1.PositionItem.Text)

Cores_DataTable.Rows(rownum).Item (WireType)= ComboBoxWireType.Text

Cores_DataTable.Rows(rownum).Item(WireGauge)= ComboBoxWireGauge.Text

Cores_DataTable.Rows(rownum).Item (WireColor)= ComboBoxWireColor.Text

结束如果



CableWireDefIdTextBox.Text = BindingNavigator1.PositionItem.Text



'Me.Cores_DataTable.AcceptChanges()'这完全隐藏它

'AddRowToDataTable()

'fixComboBoxes()

MsgBox(核心数是& Cores_DataTable.Rows.Count.ToString)

结束子



私人子BindingNavigatorMoveFirstItem_Click(sender As Object,e As EventArgs)处理BindingNavigatorMoveFirstItem.Click

Cores_Binding.MoveFirst()

CableWireDefIdTextBox.Text = BindingNavigator1.PositionItem.Text'如果删除一行,这将是错误的!从数据表中获取数字

fixComboBoxes()

End Sub



Private Sub BindingNavigatorMovePreviousItem_Click(sender as Object,e作为EventArgs)处理BindingNavigatorMovePreviousItem.Click

Cores_Binding.MovePrevious()

CableWireDefIdTextBox.Text = BindingNavigator1.PositionItem.Text'如果删除行,这将是错误的!从数据表中获取数字

fixComboBoxes()

End Sub



Private Sub BindingNavigatorMoveNextItem_Click(sender as Object,e作为EventArgs)处理BindingNavigatorMoveNextItem.Click

Cores_Binding.MoveNext()

CableWireDefIdTextBox.Text = BindingNavigator1.PositionItem.Text'如果删除行,这将是错误的!从数据表中获取数字

fixComboBoxes()

End Sub



Private Sub BindingNavigatorMoveLastItem_Click(sender as Object,e作为EventArgs)处理BindingNavigatorMoveLastItem.Click

Cores_Binding.MoveLast()

CableWireDefIdTextBox.Text = BindingNavigator1.PositionItem.Text'如果删除行,这将是错误的!从数据表中获取数字

fixComboBoxes()

End Sub



Private Sub BindingNavigatorSaveItemButton_Click(sender As Object,e正如EventArgs)处理BindingNavigatorSaveItemButton.Click

'如果用户点击保存,但现在数据表中有行,添加一行并将其与对话框中的值一起保存

'如果Cores_DataTable.Rows.Count = 0那么

'AddRowToDataTable()

'结束如果

'fixComboBoxes()

'Me.Cores_DataTable.AcceptChanges()'如果在点击保存时只有一行,它会被清零!

'Me.Validate()

结束分

结束课



我尝试过:



谷歌搜索绑定到表格的绑定导航器的例子仅在代码中无济于事。所有示例都绑定到数据库中的表。如果数据表中有零行,则黑客绑定导航器添加方法进行第二次添加,但是工具条中的计数器仍然高于行数。

I have a fairly simple little form with a tabbed container for adding some information to an SQL database. On one of the tab pages I have a few text boxes and a few combo boxes. At the top of this page is a binding navigator.
One thing that makes this a bit atypical is that rather than binding to a table in my database, it is bound (partly) to a data table I created in code in the form.
Things work mostly as expected except for one annoying issue that must be resolved.
When I click the plus sign in the navigator tool strip for the first time it does NOT add a row to my data table. It will on subsequent clicks but then the counter on the tool strip is always showing one more that what is actually in the data table. I just cant see how to fix this. Been googling for examples of binding navigators bound to tables IN CODE ONLY to no avail. All examples are bound to tables in a database. I can attach my form code if that helps (how do you attach files here?)
Please help!!

Ok, I'll see if I can paste the form code in here...

Imports System.Windows.Forms
Imports System.Data
Imports System.Data.SqlClient

Public Class CableNewForm

'define our objects for the temporary datatable
Dim Cores_DataTable As New DataTable
Dim Cores_DataRow As DataRow
Dim Cores_Binding As New BindingSource

Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
'First tab page "Properties"
Try
Using connection As New SqlClient.SqlConnection(databaseOps.connectionString)
connection.Open()
Dim da As New SqlDataAdapter("select Manufacturer from Manufacturer", connection)

'The combo boxes are bound to the respective tables in the SQL database
Dim manufacturer_dt As New DataTable
da.Fill(manufacturer_dt)
If manufacturer_dt.Rows.Count > 0 Then
Me.ComboBoxManufacturer.DataSource = manufacturer_dt
Me.ComboBoxManufacturer.DisplayMember = "Manufacturer"
Me.ComboBoxManufacturer.ValueMember = "Manufacturer"
Me.ComboBoxManufacturer.SelectedIndex = 0
End If

da.SelectCommand.CommandText = "select libraryname from library"
Dim library_dt As New DataTable
da.Fill(library_dt)
If library_dt.Rows.Count > 0 Then
Me.ComboBoxLibrary.DataSource = library_dt
Me.ComboBoxLibrary.DisplayMember = "libraryname"
Me.ComboBoxLibrary.ValueMember = "libraryname"
Me.ComboBoxLibrary.SelectedIndex = 0
End If

da.SelectCommand.CommandText = "select Supplier from Supplier"
Dim supplier_dt As New DataTable
da.Fill(supplier_dt)
If supplier_dt.Rows.Count > 0 Then
Me.ComboBoxSupplier.DataSource = supplier_dt
Me.ComboBoxSupplier.DisplayMember = "Supplier"
Me.ComboBoxSupplier.ValueMember = "Supplier"
Me.ComboBoxSupplier.SelectedIndex = 0
End If

da.SelectCommand.CommandText = "select cabletypeName from cabletypes"
Dim cabletype_dt As New DataTable
da.Fill(cabletype_dt)
If cabletype_dt.Rows.Count > 0 Then
Me.ComboBoxCableType.DataSource = cabletype_dt
Me.ComboBoxCableType.DisplayMember = "cabletypeName"
Me.ComboBoxCableType.ValueMember = "cabletypeName"
Me.ComboBoxCableType.SelectedIndex = 0
End If

da.SelectCommand.CommandText = "select colorcode from colors"
Dim cablecolor_dt As New DataTable
da.Fill(cablecolor_dt)
If cablecolor_dt.Rows.Count > 0 Then
Me.ComboBoxCableColor.DataSource = cablecolor_dt
Me.ComboBoxCableColor.DisplayMember = "colorcode"
Me.ComboBoxCableColor.ValueMember = "colorcode"
Me.ComboBoxCableColor.SelectedIndex = 0
End If
End Using

'Set some defaults on the properties tab page
InsertedUserTextBox.Text = currentUser
InsertedDateTimePicker.Text = DateTime.Now.ToString("yyyy/MM/dd")
UpdatedDateTimePicker.Text = DateTime.Now.ToString("yyyy/MM/dd")
NumberOfCoresTextBox.Text = "0"
HasShieldCheckBox.Checked = False
HasGroundCheckBox.Checked = False
CableDefIdTextBox.Text = "-1"

Catch sqex As SqlClient.SqlException
MsgBox("Sql Exception: " & sqex.Message)
Catch ex As DataException
MsgBox(ex.Message)
End Try

'Second tab page "Cores"
Try
Using connection As New SqlClient.SqlConnection(databaseOps.connectionString)
connection.Open()

'Again, the combo boxes are bound to tables in the database
Using wiretype_da As New SqlDataAdapter("select wiretypename from wiretypes", connection)
Dim wiretype_dt As New DataTable
wiretype_da.Fill(wiretype_dt)
If wiretype_dt.Rows.Count > 0 Then
Me.ComboBoxWireType.DataSource = wiretype_dt
Me.ComboBoxWireType.DisplayMember = "wiretypename"
Me.ComboBoxWireType.ValueMember = "wiretypename"
Me.ComboBoxWireType.SelectedIndex = 0
End If
End Using

Using wiregauge_da As New SqlDataAdapter("select wiregaugecode from wiregauges", connection)
Dim wiregauge_dt As New DataTable
wiregauge_da.Fill(wiregauge_dt)
If wiregauge_dt.Rows.Count > 0 Then
Me.ComboBoxWireGauge.DataSource = wiregauge_dt
Me.ComboBoxWireGauge.DisplayMember = "wiregaugecode"
Me.ComboBoxWireGauge.ValueMember = "wiregaugecode"
Me.ComboBoxWireGauge.SelectedIndex = 0
End If
End Using

Using wirecolor_da As New SqlDataAdapter("select colorcode from colors", connection)
Dim wirecolor_dt As New DataTable
wirecolor_da.Fill(wirecolor_dt)
If wirecolor_dt.Rows.Count > 0 Then
Me.ComboBoxWireColor.DataSource = wirecolor_dt
Me.ComboBoxWireColor.DisplayMember = "colorcode"
Me.ComboBoxWireColor.ValueMember = "colorcode"
Me.ComboBoxWireColor.SelectedIndex = 0
End If
End Using
End Using

'Set some defaults on the cores tab page
CableDefIdTextBox1.Text = "-1"

Catch sqex As SqlClient.SqlException
MsgBox("Sql Exception: " & sqex.Message)
Catch ex As DataException
MsgBox(ex.Message)
End Try

'Create a temporary datatable for containing cable cores.
'When the "ok" button is clicked, these will be written to the database
Cores_DataTable.Columns.Add("CableWireDefId", Type.GetType("System.Int32"))
Cores_DataTable.Columns.Add("WireType", Type.GetType("System.String"))
Cores_DataTable.Columns.Add("CableDefId", Type.GetType("System.Int32"))
Cores_DataTable.Columns.Add("WireGauge", Type.GetType("System.String"))
Cores_DataTable.Columns.Add("WireGroup", Type.GetType("System.String"))
Cores_DataTable.Columns.Add("WireColor", Type.GetType("System.String"))
Cores_DataTable.Columns.Add("Voltage", Type.GetType("System.Double"))
Cores_DataTable.Columns.Add("Attenuation", Type.GetType("System.Double"))
Cores_DataTable.Columns.Add("Resistance", Type.GetType("System.Double"))
'Cores_DataTable.Constraints.Add("me", Cores_DataTable.Columns.Item("Cablewiredefid"), primaryKey:=True)

'Bind the datatable to the navigator object
Cores_Binding.DataSource = Cores_DataTable
Me.BindingNavigator1.BindingSource = Cores_Binding

'Bind the text fields
CableWireDefIdTextBox.DataBindings.Add("Text", Cores_Binding, "CableWireDefId")
WireGroupTextBox.DataBindings.Add("Text", Cores_Binding, "WireGroup")
VoltageTextBox.DataBindings.Add("Text", Cores_Binding, "Voltage")
AttenuationTextBox.DataBindings.Add("Text", Cores_Binding, "Attenuation")
ResistanceTextBox.DataBindings.Add("Text", Cores_Binding, "Resistance")
End Sub

Private Sub KillButton_Click(sender As Object, e As EventArgs) Handles KillButton.Click
Me.Close()
End Sub

Private Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
'Here the new cable will be added to the database, its cores will come from our
'temporary data table defined in this form.
Me.Close()
End Sub

Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged
Dim cabletype_index As Integer = 0

'Update the number of cores on the properties sheet
NumberOfCoresTextBox.Text = Cores_DataTable.Rows.Count
If Cores_DataTable.Rows.Count > 1 Then
cabletype_index = Me.ComboBoxCableType.FindString("Cable")
Me.ComboBoxCableType.SelectedIndex = cabletype_index
End If

'if one of the cores is a shield or ground, check the boxes in the properties tab
For Each row As DataRow In Cores_DataTable.Rows
If row.Item("WireType") = "Shield" Then
HasShieldCheckBox.Checked = True
cabletype_index = Me.ComboBoxCableType.FindString("Cable, shielded")
Me.ComboBoxCableType.SelectedIndex = cabletype_index
End If
If row.Item("WireType") = "Ground" Then
HasGroundCheckBox.Checked = True
End If
Next
End Sub

'Binding navigator overrides---------------------------------------------------------------------------------------------------

Private Sub fixComboBoxes()
'get wire specs by ID
Dim type_index As Integer = 0
'temporary vars that I can look at while debugging, to be removed later.
Dim temp As String = String.Empty
Dim temp2 As Object

For Each row As DataRow In Cores_DataTable.Rows
temp = row.Item("CableWireDefId").ToString()
If CableWireDefIdTextBox.Text = temp Then
temp2 = row.Item("WireType")
type_index = ComboBoxWireType.FindString(row.Item("WireType")) 'this can be null! fix it!
ComboBoxWireType.SelectedIndex = type_index
temp2 = row.Item("WireGauge")
type_index = ComboBoxWireGauge.FindString(row.Item("WireGauge"))
ComboBoxWireGauge.SelectedIndex = type_index
temp2 = row.Item("WireColor")
type_index = ComboBoxWireColor.FindString(row.Item("WireColor"))
ComboBoxWireColor.SelectedIndex = type_index
End If
Next
End Sub

Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorAddNewItem.Click
Dim rownum As Int16 = (Convert.ToInt16(BindingNavigator1.PositionItem.Text) - 1)
'for some reason, this is not adding to cores_dt the first time it is clicked,
'but it does increment the counter on the navigator strip to 1
Cores_Binding.AddNew()
'this will add add a row to cores_dt if the first click didnt, but then it increments the
'counter again which will say 2 even though there is only 1 row in the datatable
If Cores_DataTable.Rows.Count = 0 Then
'Cores_Binding.AddNew()
End If
'need to manually add the selected index of comboboxes since they are not bound.
If Cores_DataTable.Rows.Count > 0 Then
Cores_DataTable.Rows(rownum).Item("CableWireDefId") = Convert.ToInt32(BindingNavigator1.PositionItem.Text)
Cores_DataTable.Rows(rownum).Item("WireType") = ComboBoxWireType.Text
Cores_DataTable.Rows(rownum).Item("WireGauge") = ComboBoxWireGauge.Text
Cores_DataTable.Rows(rownum).Item("WireColor") = ComboBoxWireColor.Text
End If

CableWireDefIdTextBox.Text = BindingNavigator1.PositionItem.Text

'Me.Cores_DataTable.AcceptChanges() 'this hoses it totally
'AddRowToDataTable()
'fixComboBoxes()
MsgBox("core count is " & Cores_DataTable.Rows.Count.ToString)
End Sub

Private Sub BindingNavigatorMoveFirstItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorMoveFirstItem.Click
Cores_Binding.MoveFirst()
CableWireDefIdTextBox.Text = BindingNavigator1.PositionItem.Text 'This will be wrong if a row is deleted! get number from datatable
fixComboBoxes()
End Sub

Private Sub BindingNavigatorMovePreviousItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorMovePreviousItem.Click
Cores_Binding.MovePrevious()
CableWireDefIdTextBox.Text = BindingNavigator1.PositionItem.Text 'This will be wrong if a row is deleted! get number from datatable
fixComboBoxes()
End Sub

Private Sub BindingNavigatorMoveNextItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorMoveNextItem.Click
Cores_Binding.MoveNext()
CableWireDefIdTextBox.Text = BindingNavigator1.PositionItem.Text 'This will be wrong if a row is deleted! get number from datatable
fixComboBoxes()
End Sub

Private Sub BindingNavigatorMoveLastItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorMoveLastItem.Click
Cores_Binding.MoveLast()
CableWireDefIdTextBox.Text = BindingNavigator1.PositionItem.Text 'This will be wrong if a row is deleted! get number from datatable
fixComboBoxes()
End Sub

Private Sub BindingNavigatorSaveItemButton_Click(sender As Object, e As EventArgs) Handles BindingNavigatorSaveItemButton.Click
'if the user hits save, but there are now rows in the datatable, add a row and save it with the values in the dialog box
'If Cores_DataTable.Rows.Count = 0 Then
'AddRowToDataTable()
'End If
'fixComboBoxes()
'Me.Cores_DataTable.AcceptChanges() ' if there is only one row when hitting save, it gets zeroed out!
'Me.Validate()
End Sub
End Class

What I have tried:

Been googling for examples of binding navigators bound to tables IN CODE ONLY to no avail. All examples are bound to tables in a database. Hacked the binding navigator "add" method to do a second add if there are zero rows in the data table, works but the counter in the tool strip is still higher than the number of rows.

推荐答案

您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候您接近一个错误。



如果没有共享相关代码,请不要期待更多帮助。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Do not expect much more help without sharing related code.


您的假设我不知道或使用调试器是不正确的
your assumption that I do not know or use the debugger is incorrect


这篇关于绑定导航器不会在第一次单击时添加记录,但会在后续单击时添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆