好吧,我已经为我的第一个真正的 VB 项目工作了几个星期。我学到了很多东西,但我对这一点感到困惑。
一点背景知识,该应用程序保存来自多台机器的停机时间信息,并将详细信息保存到 SQL 数据库中。我需要从 SQL 表中提取这些数据,以便主管更新一些缺失的信息(停机原因)。我用 datagridview 创建了一个数据源和数据集。我能够使用绑定导航器保存更新的停机原因。
现在我想用复选框过滤我的数据(即第一班,第二班等)。我向后退了一步,创建了一个数据视图,删除了绑定源,并用数据视图信息填充了数据网格视图。过滤器都在工作,但我无法再将信息保存回数据库。我想知道我是否有错误绑定的东西,或者绑定导航器的代码是否错误。
我已经包含了整个表单的代码。请原谅我的经验不足。感谢您提供的任何帮助!
Public Class DataEntry2
Private Sub ProductionDownTimeTableBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles ProductionDownTimeTableBindingNavigatorSaveItem.Click
Me.Validate()
Me.ProductionDownTimeTableBindingSource.EndEdit()
'Me.TableAdapterManager.UpdateAll(Me.ProductionDownTimeDataSet)
Me.TableAdapterManager.UpdateAll(Me.ProductionDownTimeDataSet)
End Sub
Private Sub DataEntry2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'ProductionDownTimeDataSet.ProductionDownTimeTable' table. You can move, or remove it, as needed.
'Me.ProductionDownTimeTableTableAdapter.Fill(Me.ProductionDownTimeDataSet.ProductionDownTimeTable)
End Sub
Private Sub DataEntry2_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
Selection.Close()
SelectDataByLineForm.Close()
End Sub
Private Sub xP2PopulateDataBtn_Click(sender As Object, e As EventArgs) Handles xP2PopulateDataBtn.Click
'Create Connection and Dataview
Dim connetionString As String
Dim connection As SqlConnection
Dim command As SqlCommand
Dim adapter As New SqlDataAdapter
Dim ds As New DataSet
Dim dv As DataView
Dim sql As String
connetionString = "Data Source=Controls-PC;Initial Catalog=ProductionDownTime;User ID=XX;Password=XXXX"
sql = "Select * from ProductionDownTimeTable"
connection = New SqlConnection(connetionString)
Try
connection.Open()
command = New SqlCommand(sql, connection)
adapter.SelectCommand = command
adapter.Fill(ds, "Create DataView")
adapter.Dispose()
command.Dispose()
connection.Close()
'dv = ds.Tables(0).DefaultView
dv = ds.Tables(0).AsDataView
'Filters for a specific string (word) in a specicfic column.
'dv.RowFilter = String.Format("DTEventReason Like '%{0}%'", "Engineering")
'Filters for "Null" in a specicfic column.
'dv.RowFilter = "DTEventReason is Null"
'Filters looks for any column that contains NULL
'dv.RowFilter = ("DTReasonBadgeNo is Null Or DTEventReason Is Null Or DTReasonDateTime is Null")
'Filters for a shift number
'dv.RowFilter = "Shift = 2"
'Filters for LineID formatted as a string
'dv.RowFilter = String.Format("LineID Like '%{0}%'", "1N")
'Filters for LineID formatted as a string numbers only
'dv.RowFilter = String.Format("[LineID]= '1'")
'filters for multiple criteria
'dv.RowFilter = ("(DTReasonBadgeNo Is Null Or DTEventReason Is Null Or DTReasonDateTime Is Null)")
'dv.RowFilter = dv.RowFilter & "and Shift = 1"
'dv.RowFilter = dv.RowFilter & String.Format("and [LineID]= '2'")
'CheckBox Filtering code=Search for Incomplete Cells Line 2 Only
If xP2IncompleteCellsChkBox.Checked = True And _
xP2FirstShiftChkBox.Checked = False And _
xP2SecondShiftChkBox.Checked = False Then
dv.RowFilter = ("(DTReasonBadgeNo Is Null Or DTEventReason Is Null Or DTReasonDateTime Is Null)")
dv.RowFilter = dv.RowFilter & String.Format("and [LineID]= '2'")
dv.Sort = "ProductionUpDateTime"
End If
'CheckBox Filtering code=Search for Incomplete Cells on first and second shift only Line 2 Only
If xP2IncompleteCellsChkBox.Checked = True And _
xP2FirstShiftChkBox.Checked = True And _
xP2SecondShiftChkBox.Checked = True Then
dv.RowFilter = ("(DTReasonBadgeNo Is Null Or DTEventReason Is Null Or DTReasonDateTime Is Null)")
dv.RowFilter = dv.RowFilter & "and Shift=1 or Shift=2"
dv.RowFilter = dv.RowFilter & String.Format("and [LineID]= '2'")
dv.Sort = "ProductionUpDateTime"
End If
'CheckBox Filtering code=Search for Incomplete Cells on first shift only Line 2 Only
If xP2IncompleteCellsChkBox.Checked = True And _
xP2FirstShiftChkBox.Checked = True And _
xP2SecondShiftChkBox.Checked = False Then
dv.RowFilter = ("(DTReasonBadgeNo Is Null Or DTEventReason Is Null Or DTReasonDateTime Is Null)")
dv.RowFilter = dv.RowFilter & "and Shift = 1"
dv.RowFilter = dv.RowFilter & String.Format("and [LineID]= '2'")
dv.Sort = "ProductionUpDateTime"
End If
'CheckBox Filtering code=Search for Incomplete Cells on second shift only Line 2 Only
If xP2IncompleteCellsChkBox.Checked = True And _
xP2FirstShiftChkBox.Checked = False And _
xP2SecondShiftChkBox.Checked = True Then
dv.RowFilter = ("(DTReasonBadgeNo Is Null Or DTEventReason Is Null Or DTReasonDateTime Is Null)")
dv.RowFilter = dv.RowFilter & "and Shift = 2"
dv.RowFilter = dv.RowFilter & String.Format("and [LineID]= '2'")
dv.Sort = "ProductionUpDateTime"
End If
'CheckBox Filtering Code=Incomplete and Complete Cells on First Shift Only Line 2 Only
If xP2IncompleteCellsChkBox.Checked = False And _
xP2FirstShiftChkBox.Checked = True And _
xP2SecondShiftChkBox.Checked = False Then
dv.RowFilter = "Shift = 1"
dv.RowFilter = dv.RowFilter & String.Format("and [LineID]= '2'")
dv.Sort = "ProductionUpDateTime"
End If
'CheckBox Filtering Code=Incomplete and Complete Cells on Second Shift Only Line 2 Only
If xP2IncompleteCellsChkBox.Checked = False And _
xP2FirstShiftChkBox.Checked = False And _
xP2SecondShiftChkBox.Checked = True Then
dv.RowFilter = "Shift = 2"
dv.RowFilter = dv.RowFilter & String.Format("and [LineID]= '2'")
dv.Sort = "ProductionUpDateTime"
End If
'CheckBox Filtering Code=Show All Line 2 Only
If xP2IncompleteCellsChkBox.Checked = False And _
xP2FirstShiftChkBox.Checked = False And _
xP2SecondShiftChkBox.Checked = False Then
dv.RowFilter = "1 = 1"
dv.RowFilter = dv.RowFilter & String.Format("and [LineID]= '2'")
dv.Sort = "ProductionUpDateTime"
End If
'CheckBox Filter Code Show all Data from First and Second Shift Line 2 Only
If xP2IncompleteCellsChkBox.Checked = False And _
xP2FirstShiftChkBox.Checked = True And _
xP2SecondShiftChkBox.Checked = True Then
dv.RowFilter = "Shift=1 or Shift=2"
dv.RowFilter = dv.RowFilter & String.Format("and [LineID]= '2'")
dv.Sort = "ProductionUpDateTime"
End If
ProductionDownTimeTableDataGridView.DataSource = ProductionDownTimeTableBindingSource
ProductionDownTimeTableBindingSource.DataSource = dv
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class