2015-11-10 119 views

回答

1

然後用正確的類型:

For Each Item As DataRowView In CheckedListBox_subject.CheckedItems 
    Dim text As String = Item(0).ToString() ' or any other column, you can also use the string overload 
    MsgBox(text) 
Next 
+0

謝謝。這行得通 !! –

0

以下顯示了當數據源是一個DataTable

更多看到我code article微軟。

Public Class Form1 
    ''' <summary> 
    ''' clbCheckedListBox is our CheckedListBox 
    ''' </summary> 
    ''' <param name="sender"></param> 
    ''' <param name="e"></param> 
    ''' <remarks></remarks> 
    Private Sub Form1_Load(sender As Object, e As EventArgs) _ 
     Handles MyBase.Load 

     Dim dt As New DataTable With {.TableName = "MyTable"} 

     dt.Columns.Add(New DataColumn With {.ColumnName = "Identifier", 
              .DataType = GetType(Int32), 
              .AutoIncrement = True, 
              .AutoIncrementStep = 100, 
              .AutoIncrementSeed = 100}) 

     dt.Columns.Add(New DataColumn With {.ColumnName = "ItemName", 
              .DataType = GetType(String)}) 

     dt.Columns.Add(New DataColumn With {.ColumnName = "ExtraData", 
              .DataType = GetType(String)}) 


     dt.Rows.Add(New Object() {Nothing, "One", "Extra 1"}) 
     dt.Rows.Add(New Object() {Nothing, "Two", "Extra 2"}) 
     dt.Rows.Add(New Object() {Nothing, "Three", "Extra 3"}) 
     dt.Rows.Add(New Object() {Nothing, "Four", "Extra 4"}) 
     dt.Rows.Add(New Object() {Nothing, "Five", "Extra 5"}) 

     clbCheckedListBox.DataSource = dt 
     clbCheckedListBox.DisplayMember = "ItemName" 
    End Sub 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

     Dim Checked As CheckedListBox.CheckedItemCollection = 
      clbCheckedListBox.CheckedItems 

     If Checked.Count = 0 Then 
      Exit Sub 
     End If 

     ' Conventional method to react 
     For Each drv As DataRowView In Checked 

      Console.WriteLine("{0} {1}", 
           drv.Row.Field(Of Integer)("Identifier"), 
           drv.Row.Field(Of String)("ItemName"), 
           drv.Row.Field(Of String)("ExtraData") 
          ) 

     Next 
    End Sub 
End Class 
相關問題