Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I would like to ask for a help because I'm really stuck for a few days while searching for any possible hints for this.
I'm using VB 2010 Express.

I have a multiline text box (filled manually - not from the external file) with content like:
VB
A,123,abc
B,456,def
C,789,ghi

... and I need to split these data and place them into the separated rows/columns in datagridview like:
VB
      Column1   Column2   Column3
Row1   A         123      abc
Row2   B         456      def
Row3   C         789      ghi

Can anyone help me?
Many thanks in advance.

PS:
With this, I'm able only to fill first column in each row, but then I need to split these strings and place them into the remaining columns:
VB
dgvData.Rows.Clear()

Dim myList1 As New List(Of String)
myList1.AddRange(txtCompDataView.Lines)

Dim row As String()
For index = 0 To txtCompDataView.Lines.Count - 1
   row = New String() {myList1(index)}
   dgvData.Rows.Add(row)
Next
Posted
Updated 12-Jul-15 23:19pm
v2
Comments
Akhil Mittal 13-Jul-15 5:12am    
Do you want the logic to do that?
[no name] 13-Jul-15 5:20am    
I know the logic itself, but I don't know how to modify the code - how to write the source code.

change
VB
row = New String() {myList1(index)}
dgvData.Rows.Add(row)

to
VB
dgvData.Rows.Add(myList1(index).Split(New Char() {","c}))
 
Share this answer
 
Comments
[no name] 13-Jul-15 5:31am    
This is working great!
Many thanks for this!
One way would be to use a data table. Create data table with proper structure and then fill with the data by splitting the string.

So something like:
VB
Dim dt As New System.Data.DataTable 

dt.Columns.Add("Col 1")
dt.Columns.Add("Col 2")
dt.Columns.Add("Col 3")

dt.Rows.Add("A,123,abc".Split(","))
dt.Rows.Add("B,456,def".Split(","))
dt.Rows.Add("C,789,ghi".Split(","))
         
myDataGridView.DataSource = dt

Of course the data rows would come from individual inputs in your text box. The static strings are used just for an example.
 
Share this answer
 
v2
Comments
[no name] 13-Jul-15 5:32am    
Thanks for your help, but I was trying to avoid of creating of any data tables.
Anyway, really thanks.
Wendelius 13-Jul-15 5:33am    
My pleasure :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900