Nun, ich habe mehrere Wochen an meinem ersten echten VB-Projekt gearbeitet. Ich habe viel gelernt, aber ich bin auf diesem einen ratlos.VB.Net DataView Gespeichert mit Binding Navigator
Ein wenig Hintergrund, speichert die Anwendung Ausfallzeiten Informationen von mehreren Maschinen und speichert die Details in einer SQL-Datenbank. Ich musste diese Daten aus der SQL-Tabelle für die Supervisor zurückholen, um einige fehlende Informationen zu aktualisieren (Grund für den Ausfall). Ich habe eine Datenquelle und ein Dataset mit einem Datagridview erstellt. Ich konnte den aktualisierten Ausfallgrund mit dem Binding Navigator speichern.
Jetzt wollte ich meine Daten mit Checkboxen (dh 1. Schicht, 2. Schicht usw.) filtern. Ich machte einen Schritt zurück und erstellte eine Datenansicht, entfernte die Bindungsquelle und füllte die Datagridview mit den Datenansichtsinformationen. Die Filter funktionieren alle, aber ich kann die Informationen nicht mehr in der Datenbank speichern. Ich frage mich, ob ich etwas falsch gebunden habe, oder ob der Code für den verbindlichen Navigator falsch ist.
Ich habe den Code für das gesamte Formular enthalten. Bitte entschuldigen Sie meine Unerfahrenheit. Vielen Dank für Ihre Unterstützung!
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
Der BindingNavigator kann seine eigene Filterung durchführen: 'myBNav.BindingSource.Filter =" ... "' – Plutonix