解析CSV字符串并将其绑定到列表框

时间:2010-05-19 13:25:59

标签: vb.net

我在字符串数组中分割了逗号分隔值,类似于

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

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

我正在使用Attachmodel类

Public Class 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

结束班

在上面我想设置值并使用List

将其绑定到网格

1 个答案:

答案 0 :(得分: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