我有一个具有这种外观的多行文本框:
A#B C# D#E我想将我的datagridview填充到这样的东西:
_________ |A|BC|D|E|我的意思是,我想在有“#”时拆分,但我不想在datagridview多行单元datagridview 。
我试过这段代码:
Dim sup() As String = TextBox1.Text.Split(vbCr, vbLf, vbTab, " "c, "#") DataGridView1.Rows.Add(sup(0), sup(1), sup(2),sup(3))但它说它超出了界限......谢谢!
编辑:“索引超出范围异常”错误。
如果我将文本框值粘贴到microsoft word,它们会像这样:
I have a multi-line textbox with this appearance:
A#B C# D#Eand I want to populate my datagridview to something like this:
_________ |A|BC|D|E|I mean, I want to split when there's a "#" , but I don't want multi-line cells in datagridview.
I tried this code:
Dim sup() As String = TextBox1.Text.Split(vbCr, vbLf, vbTab, " "c, "#") DataGridView1.Rows.Add(sup(0), sup(1), sup(2),sup(3))but it says it goes outta bounds ... Thanks!
edit: "Index out of range exception" Error.
If i paste the textbox values to microsoft word they come like this:
最满意答案
如果您确定要将文本框拆分为3#,则可以在datagridview中创建3列后使用类似的内容
Dim myStr As String Dim substring As String Dim strArray() As String Dim columnInt as Integer = 0 myStr = Textbox1.Text strArray = myStr.Split("#") For i = 0 to strArray.Length - 1 Datagridview.Rows(0).Cells(columnInt).Value = strArray(i) columnInt += 1 next由于我不太确定您希望这些数据如何准确显示,因此如果列数大于3,您可能还需要声明列数。在第一个For语句之前添加此代码:
For i = 0 to strArray.Length - 1 DataGridView.Columns.Add("YourText","YourText") Next未经测试但它应该让你在正确的位置! *编辑:测试后更新
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer4.Tick If DataGridView1.RowCount = 1 Then DataGridView1.Rows.Clear() Call dgvstuff() Timer1.Stop() End Sub Sub dgvstuff() Dim sup2 = TextBox2.Text.Replace("#", "").Replace(">", " "c) Dim sup() = sup2.Split(" "c, "#", vbCrLf, vbTab) With DataGridView1 .Rows(0).Cells(0).Value = sup(1).ToString .Rows(0).Cells(1).Value = sup(7).ToString .Rows(0).Cells(3).Value = sup(4).ToString End With End SubI really don't know why but it works like this. Anyone knows a better/cleaner way? Tks
更多推荐
发布评论