Display a csv variable in columns

  • Thread starter Thread starter Microsoft
  • Start date Start date
M

Microsoft

I have a variable that will equal something like this..

result= joe,200,male,sales


I know if I save the values to a text file and open it in EXCEL it shows up
in a nice colum format (csv)

Is there any way to simulate this on a vb.net form? So that I don't have to
save the values to a csv file and open it separately?

Thanks for the help
 
Yes you can. If you just want to see the values, you can simply call the
Split function and print the values out in a textbox.

Dim str As String = "1,2,3,4,5,6,a,b,c,d"
Dim arr() As String = str.Split(","c)

For Each s As String In arr
TextBox1.Text &= TextBox1.Text & s & vbCrLf
Next

However, if you want it formatted in columns, you can use the datagrid.
Since you cannot directly bind strings to a datagrid, you can create a class
(or structure) so that the values are correctly displayed in the datagrid.
Below is the code for this (DetailGrid is a datagrid):

Dim str As String = "1,2,3,4,5,6,a,b,c,d"
Dim arr() As String = str.Split(","c)
Dim al As ArrayList = New ArrayList
For Each s As String In arr
al.Add(New TestClass(s))
Next
DetailGrid.DataSource = al
Dim gs As New DataGridTableStyle
gs.MappingName = "Array"
Dim tc As New DataGridTextBoxColumn
tc.MappingName = "Value"
tc.HeaderText = "CSV Value"
tc.Width = 100
gs.GridColumnStyles.Add(tc)
DetailGrid.TableStyles.Clear()
DetailGrid.TableStyles.Add(gs)

Public Class TestClass

Private str As String

Public Sub New(ByVal s As String)
str = s
End Sub

Public ReadOnly Property Value() As String
Get
Return str
End Get
End Property

End Class



hope that helps..
Imran.
 
It almost does. I need each value to be in a separate column. In your
example each value is a separate row.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top