VB.NET DataGridview过滤自动运行

时间:2016-05-05 17:03:33

标签: vb.net datagridview filtering

几乎完成了这个项目,但还有另一个bug。当我的表单加载时,我选中我的过滤条件和复选框,然后按“填充”按钮填充DGV。用户将在几列中填写缺失的数据。过滤器仅填充需要填写数据的数据。

我的问题...如果您填写行的缺失数据并单击下一行,则前一行将被过滤并消失。它从未被保存过,所以当我重新填充时,它会返回,但是空的。

我不希望过滤器自动重新运行。我希望所有数据都保留,直到用户完成并保存。然后,再次单击“填充”按钮时,不会填充完全填充的行。 它似乎应该是一个简单的属性,但我找不到一个简单的解决方案。

提前谢谢你。 我将附上我的代码以供参考。

        Private Sub DataEntry3_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing

    'Closes all DataSets and Bindings 
    ProductionDownTimeDataSet1.Dispose()
    DownTimeCodesDataSet.Dispose()

    ProductionDownTimeTableBindingSource.Dispose()
    DTCodeDataBindingSource.Dispose()

    'Form Is Closing-Closes all other forms
    Selection.Close()
    SelectDataByLineForm.Close()
End Sub

Private Sub xP3PopulateBtn_Click(sender As Object, e As EventArgs) Handles xP3PopulateBtn.Click


    Delay(1)
    'Active Filters
    Try
        Dim strFilterP3 As String = String.Empty
        'CheckBox Filtering code=Search for Incomplete Cells Line 2 Only 
        If xP3IncompleteCellsChkBox.Checked = True And
        xP3FirstShiftChkBox.Checked = False And
        xP3SecondShiftChkBox.Checked = False Then
            strFilterP3 = ("(DTReasonBadgeNo Is Null Or DTEventReason Is Null Or DTReasonDateTime Is Null)")
            strFilterP3 = strFilterP3 & String.Format(" And [LineID]= '3'")
            ProductionDownTimeTableBindingSource.Filter = strFilterP3
            ProductionDownTimeTableBindingSource.Sort = "ProductionUpDateTime desc"
        End If

        'CheckBox Filtering code=Search for Incomplete Cells on first and second shift only Lines 1N and 1S Only 
        If xP3IncompleteCellsChkBox.Checked = True And
        xP3FirstShiftChkBox.Checked = True And
        xP3SecondShiftChkBox.Checked = True Then
            strFilterP3 = ("(DTReasonBadgeNo Is Null Or DTEventReason Is Null Or DTReasonDateTime Is Null) And (Shift = 1 Or Shift = 2)")
            strFilterP3 = strFilterP3 & String.Format(" And [LineID]= '3'")
            ProductionDownTimeTableBindingSource.Filter = strFilterP3
            ProductionDownTimeTableBindingSource.Sort = "ProductionUpDateTime desc"
        End If

        'CheckBox Filtering code=Search for Incomplete Cells on first shift only Line 2 Only 
        If xP3IncompleteCellsChkBox.Checked = True And
   xP3FirstShiftChkBox.Checked = True And
   xP3SecondShiftChkBox.Checked = False Then
            strFilterP3 = ("(DTReasonBadgeNo Is Null Or DTEventReason Is Null Or DTReasonDateTime Is Null) and Shift = 1")
            strFilterP3 = strFilterP3 & String.Format(" and [LineID]= '3'")
            ProductionDownTimeTableBindingSource.Filter = strFilterP3
            ProductionDownTimeTableBindingSource.Sort = "ProductionUpDateTime desc"
        End If

        'CheckBox Filtering code=Search for Incomplete Cells on second shift only Line 2 Only 
        If xP3IncompleteCellsChkBox.Checked = True And
   xP3FirstShiftChkBox.Checked = False And
   xP3SecondShiftChkBox.Checked = True Then
            strFilterP3 = ("(DTReasonBadgeNo Is Null Or DTEventReason Is Null Or DTReasonDateTime Is Null) and Shift = 2")
            strFilterP3 = strFilterP3 & String.Format(" and [LineID]= '3'")
            ProductionDownTimeTableBindingSource.Filter = strFilterP3
            ProductionDownTimeTableBindingSource.Sort = "ProductionUpDateTime desc"
        End If

        'CheckBox Filtering Code=Incomplete and Complete Cells on First Shift Only Line 2 Only 
        If xP3IncompleteCellsChkBox.Checked = False And
   xP3FirstShiftChkBox.Checked = True And
   xP3SecondShiftChkBox.Checked = False Then
            strFilterP3 = "Shift = 1"
            strFilterP3 = strFilterP3 & String.Format(" and [LineID]= '3'")
            ProductionDownTimeTableBindingSource.Filter = strFilterP3
            ProductionDownTimeTableBindingSource.Sort = "ProductionUpDateTime desc"

        End If

        'CheckBox Filtering Code=Incomplete And Complete Cells on Second Shift Only Line 2 Only 
        If xP3IncompleteCellsChkBox.Checked = False And
    xP3FirstShiftChkBox.Checked = False And
    xP3SecondShiftChkBox.Checked = True Then
            strFilterP3 = "Shift = 2"
            strFilterP3 = strFilterP3 & String.Format(" and [LineID]= '3'")
            ProductionDownTimeTableBindingSource.Filter = strFilterP3
            ProductionDownTimeTableBindingSource.Sort = "ProductionUpDateTime desc"
        End If


        'CheckBox Filtering Code=S how All Line 2 Only 
        If xP3IncompleteCellsChkBox.Checked = False And
  xP3FirstShiftChkBox.Checked = False And
  xP3SecondShiftChkBox.Checked = False Then
            strFilterP3 = "1 = 1"
            strFilterP3 = strFilterP3 & String.Format(" and [LineID]= '3'")
            ProductionDownTimeTableBindingSource.Filter = strFilterP3
            ProductionDownTimeTableBindingSource.Sort = "ProductionUpDateTime desc"

        End If

        'CheckBox Filter Code Show all Data from First and Second Shift Line 2 Only  
        If xP3IncompleteCellsChkBox.Checked = False And
            xP3FirstShiftChkBox.Checked = True And
            xP3SecondShiftChkBox.Checked = True Then
            strFilterP3 = ("(Shift = 1 Or  Shift = 2)")
            strFilterP3 = strFilterP3 & String.Format(" And [LineID]= '3'")
            ProductionDownTimeTableBindingSource.Filter = strFilterP3
            ProductionDownTimeTableBindingSource.Sort = "ProductionUpDateTime desc"
        End If

        'This line of code loads data into the 'ProductionDownTimeDataSet.ProductionDownTimeTable' table.

        Me.ProductionDownTimeTableTableAdapter.Fill(Me.ProductionDownTimeDataSet1.ProductionDownTimeTable)



    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Sub

Private Sub xP3GotoSelectionScreenButton_Click(sender As Object, e As EventArgs) Handles xP3GotoSelectionScreenButton.Click
    'Opens the Production Line Selection Form 
    Selection.Show()
    Me.Hide()
End Sub

Private Sub xP3DETimer_Tick(sender As Object, e As EventArgs) Handles xP3DETimer.Tick
    'Read the Login status from the PLC and Display the User Logged In as the text
    Dim L3Login As String
    L3Login = EthernetIPforSLCMicroCom1.Read("ST20:0")
    xP3DELoginLabel.Text = L3Login

    'Disables changing the filter unless engineering is logged in
    If L3Login = "Engineering" Then
        xP3IncompleteCellsChkBox.Visible = True
    Else
        xP3IncompleteCellsChkBox.Visible = False

    End If
End Sub

Private Sub ProductionDownTimeTableBindingNavigatorSaveItem_Click_1(sender As Object, e As EventArgs) Handles ProductionDownTimeTableBindingNavigatorSaveItem.Click
    'DataGridView-Binding Navigtor Save Button 
    Try
        Me.Validate()
        Me.ProductionDownTimeTableBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.ProductionDownTimeDataSet1)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Private Sub ProductionDownTimeTableDataGridView_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles ProductionDownTimeTableDataGridView.DataBindingComplete
    'After Binding the DataSource to the DatgridView, This fills the Combobox. Previous to waiting for this event, I had issues with
    '"System.ArgumentException:DatagridViewComboBoxCell value not valid" errors at population. 
    Me.DTCodeDataTableAdapter.Fill(Me.DownTimeCodesDataSet.DTCodeData)

    'Enables the time/date stamp when a cell is changed in the DataGridView 
    allDoneLoading = True

End Sub


Dim allDoneLoading As Boolean = False
Dim DateTimeColumnIndex As Integer = 1

Private Sub ProductionDownTimeTableDataGridView_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles ProductionDownTimeTableDataGridView.CellValueChanged

    Try
        'AllDoneLoading keeps the code from triggering during the loading of the Datagridview and erroring 

        If allDoneLoading = True Then

            If (e.ColumnIndex <> DateTimeColumnIndex) Then
                For z As Integer = 0 To ProductionDownTimeTableDataGridView.ColumnCount - 1
                    If ProductionDownTimeTableDataGridView.Item(z, e.RowIndex).Value Is Nothing Then
                        Return
                    End If
                    ProductionDownTimeTableDataGridView.Item(10, e.RowIndex).Value = DateTime.Now
                Next
            End If
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Sub

结束班

0 个答案:

没有答案