Transfer data from Datagridview to text box

jewel 841 Reputation points
2022-04-19T17:23:19.01+00:00

I have a column in datagridview called Name - Product. There are some data in the row such as- apple, orange, pineapple, grape, litchi
I would like to see these data through the button click event in the Multiline Text box-1 as below.
Apples,
Orange,
Pineapple,
Grapes,
Litchi
I don't understand how this can be done.
I seek the cooperation of experts.
And thanks in advance.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,668 questions
0 comments No comments
{count} votes

Accepted answer
  1. LesHay 7,126 Reputation points
    2022-04-19T17:59:49.157+00:00

    Hi

    Here is one way to do it. A stand alone example.

    194404-111.png

    Option Strict On  
    Option Explicit On  
    Public Class Form1  
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        ' add some test data  
        With DataGridView1  
          .Rows.Add(Nothing, "Apple")  
          .Rows.Add(Nothing, "Orange")  
          .Rows.Add(Nothing, "Pineapple")  
          .Rows.Add(Nothing, "Grapes")  
          .Rows.Add(Nothing, "Litchi")  
        End With  
      End Sub  
      
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
        TextBox1.Clear()  
        Dim comma As String = ","  
        For i As Integer = 0 To DataGridView1.Rows.Count - 1  
          If Not i = DataGridView1.NewRowIndex Then  
            Dim s As String = DataGridView1("Column2", i).Value.ToString  
            If i < DataGridView1.NewRowIndex - 1 Then  
              TextBox1.Text &= s & ","c & vbCrLf  
            Else  
              TextBox1.Text &= s  
            End If  
          End If  
        Next  
      End Sub  
      
    End Class  
    

0 additional answers

Sort by: Most helpful