Please, read my comment to the question first.
There's several issues with your code. Well...
This code:
DataGridView1.Rows.Add(sr.ReadLine.Split(CChar(vbTab)))
adds rows to the datagridview component.
And this one:
DataGridView1.DataSource = dt
removes previous data and binds data with datatable.
You have to decide what method you want to use to display data: a) unbound or b) bound DataGridView.
Ad a)
You adds data manually, row by row. You do that in
Button1_Click
event.
For further details, please see:
Walkthrough: Creating an Unbound Windows Forms DataGridView Control | Microsoft Docs[
^]
Ad b)
You make a datasource and bind it with DataGridView component. You do that in
Button2_Click
event.
More:
How to: Bind data to the Windows Forms DataGridView control | Microsoft Docs[
^]
How to: Autogenerate Columns in a Data-Bound Windows Forms DataGridView Control | Microsoft Docs[
^]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using ouvrir As New OpenFileDialog
If ouvrir.ShowDialog = DialogResult.OK Then BindData(ouvrir.FileName)
End Using
End Sub
Non-linq solution:
Private Sub BindData(sFileName As String)
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() _
{
New DataColumn("Command", Type.GetType("System.String")), _
New DataColumn("Article", Type.GetType("System.String")), _
New DataColumn("Qte", Type.GetType("System.String")), _
New DataColumn("PU", Type.GetType("System.String")), _
New DataColumn("Designation", Type.GetType("System.String")) _
})
Using sr As New StreamReader(sFileName)
While Not sr.EndOfStream
dt.Rows.Add(sr.ReadLine.Split(New String(){vbTab}, StringSplitOptions.RemoveEmptyEntries))
End While
End Using
With DataGridView1
.AutoGenerateColumns = True
.DataSource = dt
End With
End Sub
Linq solution:
Private Sub BindData(sFileName As String)
Dim lines As String() = File.ReadAllLines(sFileName)
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() _
{
New DataColumn("Command", Type.GetType("System.String")), _
New DataColumn("Article", Type.GetType("System.String")), _
New DataColumn("Qte", Type.GetType("System.String")), _
New DataColumn("PU", Type.GetType("System.String")), _
New DataColumn("Designation", Type.GetType("System.String")) _
})
dt = lines _
.Select(Function(x) dt.LoadDataRow(x.Split(New String(){vbTab}, StringSplitOptions.RemoveEmptyEntries), False)) _
.CopyToDataTable()
With DataGridView1
.AutoGenerateColumns = True
.DataSource = dt
End With
End Sub
Note #1: i have no idea how the data from Sql Server are related to the data from text file, so - you have to use your logic to fill-in
Designation
column.
Note #2: above code is not optimal, but it should provide you a way how to achieve your goal.
Note #3: i'd strongly recommend to use
Using Statement (Visual Basic) | Microsoft Docs[
^]. Please, read related article to find out why...
Good luck!