0

好吧,我已经为我的第一个真正的 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
4

1 回答 1

0

DataView除非您需要相同的多个视图,否则很少需要显式创建DataTable. EveryDataTable已经DataView通过它的DefaultView属性与 a 相关联。当您绑定 aDataTable时,例如直接绑定到 aDataGridView或 a BindingSource,暴露的数据实际上来自于DefaultView,这就是您可以通过单击网格中的列标题对数据进行排序的原因。

如果您已将 a 绑定DataTable到 a BindingSourcethen,正如@Plutonix 建议的那样,您可以BindingSource使用它的SortFilter属性通过它对数据进行排序和过滤。这些与底层的Sort和属性具有相同的效果,即绑定的。RowFilterDataViewDefaultViewDataTable

于 2016-04-28T01:37:50.993 回答