代码之家  ›  专栏  ›  技术社区  ›  user240141

解析CSV字符串并将其绑定到listbox

  •  0
  • user240141  · 技术社区  · 16 年前

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

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

    公共类附件模型

    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
    

    结束类

    1 回复  |  直到 16 年前
        1
  •  0
  •   user240141 user240141    16 年前

    我想以编程方式将CSV字符串与listbox绑定

    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