将逗号分隔的字符串转换为 VB.NET 中的数据表行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34996328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting a comma delimited string to a datatable row in VB.NET
提问by user47368
I am trying to convert a string with data separated by commas into the first row of a datatable. The datatable is otherwise empty.
我正在尝试将数据以逗号分隔的字符串转换为数据表的第一行。否则数据表为空。
The datatable will then need to populate into datagridview, however nothing appears in it.
然后需要将数据表填充到 datagridview 中,但其中没有任何内容。
Dim plt As New System.Data.DataTable
Dim rowData As String() = output.Split(New Char() {","}, StringSplitOptions.RemoveEmptyEntries)
Dim row As DataRow = PLT.NewRow()
dataGridView1.DataSource = PLT
I don't get any errors, but it's also not populating the gridview so I don't know if it's just failing to populate or if the table itself never got populated from the string.
我没有收到任何错误,但它也没有填充 gridview,所以我不知道它是否只是无法填充,或者表格本身是否从未从字符串中填充。
回答by Tim Schmelter
You need to add the columns and then the row:
您需要添加列,然后添加行:
Dim rowData As String() = output.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
Dim plt As New System.Data.DataTable()
For i As Int32 = 1 To rowData.Length
plt.Columns.Add(String.Format("Column {0}", i))
Next
Dim newRow As DataRow = plt.Rows.Add() ' already added now '
For col As Int32 = 0 To rowData.Length - 1
newRow.SetField(col, rowData(col))
Next
回答by Fabio
DataTableneed DataColumnfor every item of array.
DataTable需要DataColumn数组的每个项目。
If you want add array of string to DataGridViewthen add it straight.
如果你想添加字符串数组,DataGridView然后直接添加它。
Dim rowData As String() = output.Split(New Char() {","},
StringSplitOptions.RemoveEmptyEntries)
Me.dataGridView1.Rows.Add(rowData )

