2010-05-19 31 views
0

我已經分裂逗號分隔值,像這樣解析CSV串並綁定到在字符串數組列表框

str[0] ="210" 
str[1] ="abc.pdf" 
str[2] = "211" 
str[3] = "xyz.docx" 

等。請注意0​​,2,4,6,8 [偶數位置]有數字,奇數位置有字符串。

我有一個類Attachmodel

公共類AttachmentModel

Private _attachmentID As Integer = 0 
Private _attachmentPath As String = "" 

''' <summary> 
''' Get Set Attachment ID 
''' </summary> 
''' <value></value> 
''' <returns></returns> 
''' <remarks></remarks> 

Public Property AttachmentID() As Integer 
    Get 
     Return _attachmentID 
    End Get 
    Set(ByVal value As Integer) 
     _attachmentID = value 
    End Set 
End Property 

''' <summary> 
''' Get Set Attachment Path 
''' </summary> 
''' <value></value> 
''' <returns></returns> 
''' <remarks></remarks> 

Public Property AttachmentPath() As String 
    Get 
     Return _attachmentPath 
    End Get 
    Set(ByVal value As String) 
     _attachmentPath = value 
    End Set 
End Property 

末級

在上述我要設置的值,並將其綁定到網格,使用列表

+0

你的問題是什麼? – volody 2010-05-19 13:40:53

+0

謝謝,我已經解決了它的任何方式。我張貼我的答案。 – 2010-05-19 13:42:23

回答

0

我想以編程方式將CSV字符串與列表框綁定

Private Sub BindAttachmentsToListBox(ByVal collectionData As String) 
     Dim arrayString As String() 
     Dim separator As String() = {",", "\n", " "} 
     Dim attachmentList As New List(Of AttachmentModel) 
     arrayString = collectionData.ToString().Split(separator, StringSplitOptions.RemoveEmptyEntries) 

     For i As Integer = 0 To arrayString.Length - 1 
      Dim attachments As New AttachmentModel() 

      attachments.AttachmentID = Integer.Parse(arrayString(i).ToString().Trim()) 
      attachments.AttachmentPath = arrayString(i + 1).ToString.Trim() 

      attachmentList.Add(attachments) 
      i = i + 1 
     Next 

     lbAttachments.DataSource = attachmentList 
     lbAttachments.DisplayMember = "AttachmentPath" 
     lbAttachments.ValueMember = "AttachmentID" 


    End Sub