Databound Checkedlistbox - Check certain boxes

  • Thread starter Thread starter aaapaul
  • Start date Start date
A

aaapaul

VS 2005

I have a databound checkedlistbox:

With Me.LCheckedlistbox1
.BeginUpdate()
.Sorted = True
.DataSource = objB.Benutzertabelle
.DisplayMember = "Nachname"
.ValueMember = "Persno"
.EndUpdate()
End With

The selected indices I can get with:

For Each objView As DataRowView In
Me.LCheckedlistbox1.CheckedItems
MessageBox.Show(objView(Me.LCheckedlistbox1.ValueMember))
Next

But how can I set checked items by indeces:

E.g. I am having the value members 'A','B','X'

Something like this
For intH As Integer = 0 To Me.LCheckedlistbox1.Items.Count - 1
if ... then setitemchecked....

Next

Thanks
aaapaul
 
I did it by this way

Imports System.Windows.Forms
Public Class lCheckedlistbox
Inherits CheckedListBox

''' <summary>
''' An Datatable binden über
''' With Me.LCheckedlistbox1
''' .BeginUpdate()
''' .Sorted = True
''' .DataSource = objDa.GetData
''' .DisplayMember = "Nachname"
''' .ValueMember = "Persno"
''' .EndUpdate()
''' End With
'''
''' For Each objView As DataRowView In
Me.LCheckedlistbox1.CheckedItems
'''
MessageBox.Show(objView(Me.LCheckedlistbox1.ValueMember))
''' Next
''' </summary>
''' <remarks></remarks>

Sub New()
Me.CheckOnClick = True
End Sub

Sub LSetIndices(ByVal pdtH As DataTable)
Dim dtH As DataTable
Dim arrH() As DataRow
Dim strMember As String
dtH = CType(Me.DataSource, DataTable)
For Each drH As DataRow In pdtH.Rows
arrH = dtH.Select(Me.ValueMember & "= '" &
drH.Item(Me.ValueMember) & "'")
If arrH.GetUpperBound(0) >= 0 Then
strMember = arrH(0).Item(Me.DisplayMember)
Me.SetItemChecked(Me.FindStringExact(strMember), True)
End If

Next
End Sub

Sub LSetIndex(ByVal strValue As String)
Dim dtH As DataTable
Dim arrH() As DataRow
Dim strMember As String
dtH = CType(Me.DataSource, DataTable)
arrH = dtH.Select(Me.ValueMember & "= '" & strValue & "'")
If arrH.GetUpperBound(0) >= 0 Then
strMember = arrH(0).Item(Me.DisplayMember)
Me.SetItemChecked(Me.FindStringExact(strMember), True)
End If

End Sub
End Class
 
Back
Top