多行文本框到Datagridview(Multiline Textbox to Datagridview)

编程入门 行业动态 更新时间:2024-10-27 03:37:11
多行文本框到Datagridview(Multiline Textbox to Datagridview)

我有一个具有这种外观的多行文本框:

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#E

and 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 Sub

I really don't know why but it works like this. Anyone knows a better/cleaner way? Tks

更多推荐

本文发布于:2023-08-04 14:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417906.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Datagridview   Multiline   Textbox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!