BobNoobGuy
BobNoobGuy

Reputation: 1645

Writeline to textbox?

Stupid question here, but here is my code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dt As DataTable
    dt = CType(dgrGrid.DataSource, DataTable).Copy
    Dim drs() As DataRow = dt.Select("CustomerID = 222")
    For Each row As DataRow In drs
        For Each item As Object In row.ItemArray
            Debug.WriteLine("{0}", item)
        Next
    Next
End Sub

I can see in the debug window all my values.

Now if I need to spit out this value to a textbox, I am not sure how to do it.

The goal is to keep using itemArray to get the value.

Upvotes: 1

Views: 1268

Answers (2)

Visual Vincent
Visual Vincent

Reputation: 18310

In addition to Reza's answer, and if you're lazy like me and don't want to write Environment.NewLine everywhere, you can create an extension method for adding new lines:

Imports System.Runtime.CompilerServices

Public Module Extensions
    <Extension()> _
    Public Sub AppendLine(ByVal TargetTextBox As TextBox, ByVal Text As String)
        TargetTextBox.AppendText(Text & Environment.NewLine)
    End Sub
End Module

Usage:

For Each item As Object In row.ItemArray
    TextBox1.AppendLine(item.ToString())
Next

Upvotes: 1

Reza Aghaei
Reza Aghaei

Reputation: 125227

You can use something like this:

Dim rows = dt.Select() ' Or select using a criteria like you did
Me.TextBox1.Text = String.Join(Environment.NewLine, _
    rows.Select(Function(r) String.Join(",", r.ItemArray)))

The TextBox1 should be MultiLine to show Environment.NewLine.

Also if you are looking for something like AppendLine, use:

Me.TextBox1.AppendText("something" & Environment.NewLine)

Upvotes: 2

Related Questions