好吧,我已经在我的第一个真正的VB项目上工作了几个星期。我学到了很多东西,但我对这个很难过。
一点背景,该应用程序可以保存来自多台计算机的停机信息,并将详细信息保存到SQL数据库中。我需要从SQL表中提取数据,以便主管更新一些丢失的信息(停机原因)。我创建了一个带有datagridview的数据源和数据集。我能够使用绑定导航器保存更新的停机时间原因。
现在我想用复选框过滤我的数据(即第一班,第二班等)。我向后退了一步并创建了一个数据视图,删除了绑定源,并使用dataview信息填充了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
答案 0 :(得分:0)
除非您需要同一DataView
的多个视图,否则很少需要明确创建DataTable
。每个DataTable
已通过其DataView
属性与DefaultView
相关联。绑定DataTable
时,例如直接发送到DataGridView
或BindingSource
,公开的数据实际上来自DefaultView
,这就是您可以通过单击网格中的列标题对数据进行排序的原因。
如果您已将DataTable
绑定到BindingSource
,那么正如@Plutonix建议的那样,您可以使用BindingSource
和Sort
对Filter
数据进行排序和过滤{1}}属性。这些与基础Sort
的{{1}}和RowFilter
属性具有相同的效果,DataView
是绑定DefaultView
的{{1}}。