databasevb.netchecklistbox

Read check status of Checklistbox in vb.net bound to a databasetable


I can successfully populate a checklistbox in vb.net with data stored in a database with this code:

 Private Sub report_enter() Handles tp_report.Enter
    Dim rep As DataTable = sqlite.SelectData("SELECT field,name,obligatory from cnf_oblfields WHERE module='report'")

    clb_obl.DataSource = rep
    clb_obl.ValueMember = "field"
    clb_obl.DisplayMember = "name"
    For i = 0 To rep.Rows.Count - 1
        clb_obl.SetItemChecked(i, sqlite.Int2Bool(rep.Rows(i).Item(2)))
    Next
End Sub

Now, the user can check and uncheck some boxes. I want to store the new status of these parameters back to the table. I tried this code:

Private Sub bt_obli_save_Click(sender As Object, e As EventArgs) Handles bt_obli_save.Click
    For Each item In clb_obl.Items
        Dim row As DataRow = item.row

        MsgBox("INSERT OR REPLACE INTO cnf_oblfields ('field', 'obligatory') VALUES ('" & item.item("field").ToString & "', '" & item.item("obligatory").ToString & "')")
    Next
End Sub

My problem is, that the SUB which shall write the results back to the database gives back the original data, not the changes that i made. This is probably due to the bound datatable in the background?

Thank you for your help! Lukas


Solution

  • you have to update the datatable when you modify the checkedlistbox like so

    Private Sub clb_obl_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles clb_obl.ItemCheck
    
        DirectCast(clb_obl.Items(e.Index), DataRowView)("obligatory") = e.NewValue
    
    End Sub