解析 CSV 字符串并将其绑定到列表框 [英] Parsing CSV string and binding it to listbox

查看:26
本文介绍了解析 CSV 字符串并将其绑定到列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个字符串数组中分割了逗号分隔值,就像这样

I have splitted comma separated values in an string array, something like this

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

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

and so on. Please note 0,2,4,6,8 [even positions] are having number and odd positions are having string.

我有一个附加模型类

公共类附件模型

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

结束课程

在上面我想设置值并将其绑定到网格,使用列表

In the above i want to set the values and bind it to the grid, using List

推荐答案

我想以编程方式将 CSV 字符串与列表框绑定

I want to bind CSV string with listbox programmatically

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

这篇关于解析 CSV 字符串并将其绑定到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆