2017-03-04 54 views
0

編輯從加入數據庫時​​選擇checkedlistbox項目

您好我有這個計劃,我需要顯示值的總和從數據庫中讀取相關聯時,雙精度值。我試過這樣的:

Dim selec As String 
    Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\rose&mike\Desktop\DbSysDel3\salondbaccess.accdb") 
    Dim dt2 As New DataTable 
    selec = "" 
    con.Open() 
    For Each incheck In chcklstbx1.CheckedIndices 
     Dim valName As String 
     valName = chcklstbx1.Items.Item(incheck).ToString 
     Dim sqlstr2 As String = "SELECT Service_Fee FROM Service_Types WHERE [Service_Name] = '" & valName & "'" 
     Dim cmd As New OleDbCommand(sqlstr2, con) 
     Dim sum As Double = 0 
     Dim rdr As OleDbDataReader = cmd.ExecuteReader 
     If rdr.HasRows Then 
      While rdr.Read 
       selec += "P" + rdr("Service_Fee").ToString & ControlChars.NewLine 
       sum = sum + rdr("Service_Fee") 

      End While 
     End If 
     lblFees.Text = selec 
     lblTotal.Text = sum.ToString 
     rdr.Close() 
    Next 
    con.Close() 

但它所做的是顯示我想要添加一個一個的值時檢查。

+0

我不能看到任何你基於CheckedListBox限定查詢或循環 - 它不會出現在代碼中的任何地方。 – Plutonix

+0

爲什麼不你[編輯]你的文章,所以我們可以閱讀 – Plutonix

+0

@Plutonix是的,我已經做到了。請看一下。謝謝。 – luca

回答

0

下面是這整個事情應該怎麼處理的例子:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    Using connection As New OleDbConnection("connection string here"), 
      adapter As New OleDbDataAdapter("SELECT Name, Amount FROM MyTable", connection) 
     Dim table As New DataTable 

     adapter.Fill(table) 

     'Bind the DataTable to the CheckedListBox. 
     'Normally the DataSource should be set last but, in my experience, that can cause problems with a CheckedListBox. 
     With CheckedListBox1 
      .DataSource = table 
      .DisplayMember = "Name" 
      .ValueMember = "Amount" 
     End With 
    End Using 
End Sub 

Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck 
    'Get all the currently checked items. 
    Dim checkedItems = CheckedListBox1.CheckedItems.Cast(Of DataRowView)().ToList() 

    'Get the item that is being checked or unchecked. 
    Dim currentItem = DirectCast(CheckedListBox1.Items(e.Index), DataRowView) 

    If e.NewValue = CheckState.Checked Then 
     'The current item is being checked so add it to the list. 
     checkedItems.Add(currentItem) 
    Else 
     'The current item is being unchecked so remove it from the list. 
     checkedItems.Remove(currentItem) 
    End If 

    'Sum the Amounts from each checked item. 
    Dim sum = checkedItems.Sum(Function(drv) CDec(drv("Amount"))) 

    'Use sum here. 
    '... 
End Sub 

你起牀前的所有數據,包括所有顯示的文字和所有相關的數值。該數據可以綁定到CheckedListBox作爲DataTable,將它們全部打包在一起。

每次用戶選中或取消選中列表中的項目時,都會根據您已擁有的數據執行計算。無需返回數據庫。

關於ItemCheck事件需要注意的重要一點是,在項目被選中或未選中之前,它會被引發。這就是爲什麼代碼需要廣告當前項目或從中刪除項目的列表。

當您綁定DataTable時,每個綁定項目是DataRowView,即表格的DefaultView中的項目。你得到所有檢查的DataRowView項目,獲取它們的數值並對它們進行求和。請注意,我使用了LINQ查詢來簡潔地計算總和,但是如果您想要,可以使用循環。

Dim sum As Decimal = 0D 

For Each drv In checkedItems 
    sum += CDec(drv("Amount")) 
Next 
+0

我對VB.Net其實很新,所以我很抱歉如果我的設計不好。我會嘗試使用您給出的參考樣本,謝謝。 – luca

+0

有沒有必要道歉。當我開始時,我的應用程序設計也很糟糕。當我說某人的代碼不好時,我不會判斷。我只強調爲什麼代碼中的重大更改是有保證的。 – jmcilhinney

+0

非常感謝你@jmcilhinney它運作得非常好。但是現在我不知道如何顯示檢查項目的個別數量,這就是我使用'selec'的原因。有關如何正確執行此操作的任何建議? – luca

相關問題