25-07-23, 09:46 PM
كود :
Public Class Form1
Private Sub btnAddToGrid_Click(sender As Object, e As EventArgs) Handles btnAddToGrid.Click
' Split the lines of text in each TextBox and extract the first lines.
Dim textBox1Lines As String() = TextBox1.Text.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
Dim textBox2Lines As String() = TextBox2.Text.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
Dim textBox3Lines As String() = TextBox3.Text.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
' Find the maximum number of lines among the TextBoxes.
Dim maxLinesCount As Integer = Math.Max(Math.Max(textBox1Lines.Length, textBox2Lines.Length), textBox3Lines.Length)
' Create a DataTable to hold the data to be displayed in the DataGridView.
Dim dt As New DataTable()
dt.Columns.Add("TextBox1")
dt.Columns.Add("TextBox2")
dt.Columns.Add("TextBox3")
' Loop through each line and add the first lines to the DataTable.
For i As Integer = 0 To maxLinesCount - 1
Dim row As DataRow = dt.NewRow()
If i < textBox1Lines.Length Then
row("TextBox1") = textBox1Lines(i)
End If
If i < textBox2Lines.Length Then
row("TextBox2") = textBox2Lines(i)
End If
If i < textBox3Lines.Length Then
row("TextBox3") = textBox3Lines(i)
End If
dt.Rows.Add(row)
Next
' Bind the DataTable to the DataGridView to display the data.
DataGridView1.DataSource = dt
End Sub
End Class
