2016-04-27 49 views
0

嗯,我已經在我的第一個真正的VB項目上工作了幾個星期。我學到了很多東西,但是我很困惑這一點。VB.Net DataView保存綁定導航

稍有後臺,應用程序可以保存來自多臺機器的停機信息並將詳細信息保存到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 
+1

BindingNavigator可以進行自己的過濾:'myBNav.BindingSource.Filter =「...」' – Plutonix

回答

0

有很少需要創建一個DataView明確,除非你需要同樣DataTable的多個視圖。每DataTable已通過它的DefaultView屬性與DataView關聯。當您綁定DataTable時,例如,直接到DataGridViewBindingSource,暴露的數據實際上來自DefaultView,這就是爲什麼您能夠通過單擊網格中的列標題對數據進行排序的原因。

如果您已綁定一個DataTableBindingSource那麼,作爲@Plutonix建議,你可以進行排序,並使用其SortFilter屬性過濾通過該BindingSource數據。這些與底層DataViewSortRowFilter屬性具有相同的效果,這是綁定DataTableDefaultView

+0

感謝Plutonix和JMCilhinney!我刪除了數據視圖代碼,將所有內容都填充到數據集中,並按照您的建議使用綁定源過濾器。它正在運作。現在清理一下,然後繼續下一步!非常感激! – sds5150

+0

@ sds5150如果一切正常,請點擊複選標記以將其從「無人接聽」列表中移除。它還可以幫助其他人在Google上找到好的答案。 – Plutonix