我的目标是采取两行(FirstName和Surname)将它们转换为单个数组" FirstName,Surname"。
这是我最终放在一起的可怕代码
Private Sub Search_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DbaPatientDataSet.tblPatientData' table. You can move, or remove it, as needed.
Me.TblPatientDataTableAdapter.Fill(Me.DbaPatientDataSet.tblPatientData)
listFirst.DataSource = Me.TblPatientDataBindingSource
listFirst.DisplayMember = "FirstName"
listLast.DataSource = Me.TblPatientDataBindingSource
listLast.DisplayMember = "Surname"
Dim Lenth As Integer = Me.listFirst.Items.Count - 1
Dim count As Integer = 1
Dim ArrFirst(Lenth) As String
Dim ArrLast(Lenth) As String
For count = 1 To Lenth
ArrFirst(count) = listFirst.Items(count).ToString
ArrLast(count) = listLast.Items(count).ToString
Next count
count = 1
For count = 1 To Lenth
arrFullName(count) = ArrLast(count) & ", " & ArrFirst(count)
Next count
'Arrays Set =====================================================
但是使用这段代码我得到了一个
数组`"Sytem.Data.DataRowView, Sytem.Data.DataRowView"
"Sytem.Data.DataRowView, Sytem.Data.DataRowView"
"Sytem.Data.DataRowView, Sytem.Data.DataRowView"
"Sytem.Data.DataRowView, Sytem.Data.DataRowView"
`
如你所见 Here
必须有一种简单的方法可以将DataRows
转换为字符串,然后将concatenate
转换为数组
我将使用二进制搜索搜索此数组以查找所需名称
由于
答案 0 :(得分:1)
首先,我认为您会混淆行和列。你有2列。我直接去了全名,但我认为你可以在需要的时候将其分解。
Dim arrNames(ListBox1.Items.Count - 1) As String
For i As Integer = 0 To ListBox1.Items.Count - 1
arrNames(i) = $"{ListBox1.Items(i)} {ListBox2.Items(i)}"
Next
For Each item In arrNames
Debug.Print(item)
Next
前面带$的字符串是一个插值字符串。对String.Format进行改进。
答案 1 :(得分:0)
我知道有答案但是现在你可以直接到数据表来获得你需要的东西。
Dim arrNames(ListBox1.Items.Count - 1) As String
Dim i As Integer = 0
Dim dt As DataTable = DbaPatientDataSet.Tables(0)
For Each row As DataRow In dt.Rows
arrNames(i) = $"{row("Surname")}, {row("FirstName")}"
i += 1
Next
For Each item In arrNames
Debug.Print(item)
Next
'assume the names of your columns are Surname and FirstName
答案 2 :(得分:0)
如果我运行你的代码,我会得到你想要的结果,所以我不确定你缺少什么。尽管如此,你通过乱搞数组来制造自己的事情:)。只需直接使用数据集行 - 它们是强类型的,您可以根据需要检查空值等......就像这样;
Dim fullNames As New List(Of String) '-- or you could fill your array.
For Each row As DbaPatientDataSet.tblPatientDataRow In ds.tblPatientData
fullNames.Add(row.Surname & ", " & row.FirstName)
Next
只看你想要实现的目标,如果是我,我会在我的查询中带回格式化数据,填充数据集,即第三个FullName列。
答案 3 :(得分:0)
一直在我的脑海里。最后直接拿到了List Box。
Dim arrFullNames(ListBox1.Items.Count - 1) As String
Dim i As Integer = 0
For Each item As DataRowView In ListBox1.Items
arrFullNames(i) = $"{DirectCast(item("Surname"), String)}, {DirectCast(item("Firstname"), String)}"
i += 1
Next
For Each item As String In arrFullNames
Debug.Print(item)
Next