用逗号分割多个字符串并将字符串部分放入DataGridView [英] Split a multiple string by comma and place string parts into DataGridView

查看:241
本文介绍了用逗号分割多个字符串并将字符串部分放入DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我想请求帮助,因为我在搜索任何可能的提示时已经被困了几天。

我' m使用VB 2010 Express。



我有一个多行文本框(手动填充 - 不是来自外部文件),内容如下:

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:

A,123,abc
B,456,def
C,789,ghi



......我需要拆分这些数据和地点它们分为datagridview中的分隔行/列,如:


... and I need to split these data and place them into the separated rows/columns in datagridview like:

      Column1   Column2   Column3
Row1   A         123      abc
Row2   B         456      def
Row3   C         789      ghi



有谁能帮帮我?

非常感谢提前。



PS:

有了这个,我只能填写第一个col在每一行中,但是我需要拆分这些字符串并将它们放入剩下的列中:


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:

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

推荐答案

更改

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






to

dgvData.Rows.Add(myList1(index).Split(New Char() {","c}))


一种方法是使用数据表。创建具有适当结构的数据表,然后通过拆分字符串来填充数据。



所以类似于:

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:
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.


这篇关于用逗号分割多个字符串并将字符串部分放入DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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