VB.Net 使用绑定导航器保存的 DataView
VB.Net DataView Saved With Binding Navigator
好吧,我已经为我的第一个真正的 VB 项目工作了几个星期。我学到了很多东西,但我对这一点感到困惑。
一点背景知识,该应用程序保存多台机器的停机信息,并将详细信息保存到 SQL 数据库中。我需要从 SQL table 中提取数据,以便主管更新一些缺失的信息(停机原因)。我使用数据网格视图创建了数据源和数据集。我能够使用绑定导航器保存更新的停机原因。
现在我想用复选框过滤我的数据(即第一班、第二班等)。我向后退了一步,创建了一个数据视图,删除了绑定源,并用数据视图信息填充了数据网格视图。过滤器都在工作,但我无法再将信息保存回数据库。我想知道是不是我绑定的东西不正确,或者绑定导航器的代码是否有误。
我已经包含了整个表单的代码。
请原谅我的经验不足。
感谢您提供的任何帮助!
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
很少需要显式创建 DataView
,除非您需要同一 DataTable
的多个视图。每个 DataTable
已经通过 DefaultView
属性 与 DataView
相关联。当您绑定 DataTable
时,例如直接到 DataGridView
或 BindingSource
,暴露的数据实际上来自 DefaultView
,这就是为什么您可以通过单击列 header 来对数据进行排序在网格中。
如果您已将 DataTable
绑定到 BindingSource
,那么,正如@Plutonix 建议的那样,您可以使用 Sort
通过 BindingSource
对数据进行排序和过滤和 Filter
属性。这些与底层 DataView
的 Sort
和 RowFilter
属性具有相同的效果,即绑定 DataTable
.
的 DefaultView
好吧,我已经为我的第一个真正的 VB 项目工作了几个星期。我学到了很多东西,但我对这一点感到困惑。
一点背景知识,该应用程序保存多台机器的停机信息,并将详细信息保存到 SQL 数据库中。我需要从 SQL table 中提取数据,以便主管更新一些缺失的信息(停机原因)。我使用数据网格视图创建了数据源和数据集。我能够使用绑定导航器保存更新的停机原因。
现在我想用复选框过滤我的数据(即第一班、第二班等)。我向后退了一步,创建了一个数据视图,删除了绑定源,并用数据视图信息填充了数据网格视图。过滤器都在工作,但我无法再将信息保存回数据库。我想知道是不是我绑定的东西不正确,或者绑定导航器的代码是否有误。
我已经包含了整个表单的代码。 请原谅我的经验不足。 感谢您提供的任何帮助!
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
很少需要显式创建 DataView
,除非您需要同一 DataTable
的多个视图。每个 DataTable
已经通过 DefaultView
属性 与 DataView
相关联。当您绑定 DataTable
时,例如直接到 DataGridView
或 BindingSource
,暴露的数据实际上来自 DefaultView
,这就是为什么您可以通过单击列 header 来对数据进行排序在网格中。
如果您已将 DataTable
绑定到 BindingSource
,那么,正如@Plutonix 建议的那样,您可以使用 Sort
通过 BindingSource
对数据进行排序和过滤和 Filter
属性。这些与底层 DataView
的 Sort
和 RowFilter
属性具有相同的效果,即绑定 DataTable
.
DefaultView