Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
I have a DataGridView with DataSource from a text file

Command     Article      Qte       PU
abcd        ab17        17        1.8


Now I need to add another column from a database and I have a lot of problem with it.
While adding it show under the first column automatically and what I want to add it as new column i will make a sql requet to compare second column with database and fill the column what i want,will be like this

Command     Article     Qte       PU          Designation
abcd        ab17        17        1.8         AAAAAAAA


What I have tried:

VB.NET
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

      Dim ouvrir As New OpenFileDialog
      If ouvrir.ShowDialog = DialogResult.OK Then

          Dim sr As New StreamReader(ouvrir.FileName)
          While Not sr.EndOfStream
              DataGridView1.Rows.Add(sr.ReadLine.Split(CChar(vbTab)))
          End While
      End If
  End Sub

  Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
      Dim cmd As New SqlCommand("select Numero from DetailReceptionFrs", cn)
      Dim da As New SqlDataAdapter(cmd)
      Dim dt As New DataTable
      da.Fill(dt)
      DataGridView1.DataSource = dt

  End Sub
Posted
Updated 17-Apr-19 22:55pm
v3
Comments
[no name] 17-Apr-19 10:33am    
You join the text and data base data BEFORE you load the data grid. Create two collections from the data sources and use LINQ.
Maciej Los 17-Apr-19 15:06pm    
Based on what logic you want to add that column? Is there any related data?

When you set the DataSource, that provides the schema for the DGV - and that means you shouldn't just "add a column" to the DGV itself.
You can, just by adding it to the DGV.Columns collection:
VB
DataGridView1.Columns.Add("NameOfColumn", "Coulumn Heading Text")
But that may cause problems if you add rows and try to save it / update the source later.
You can also do it by addit it to the underlying DataTable:
Dim dt As DataTable = TryCast(DataGridView1.DataSource, DataTable)
dt.Columns.Add("Rating")
which may work better.
 
Share this answer
 
Please, read my comment to the question first.

There's several issues with your code. Well...

This code:
VB
DataGridView1.Rows.Add(sr.ReadLine.Split(CChar(vbTab)))

adds rows to the datagridview component.
And this one:
VB
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[^]



VB
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:
VB
Private Sub BindData(sFileName As String)

	'create a datatable object to bind data to DataGridView component
Dim dt As DataTable = New DataTable()
	'add columns 
	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")) _
		})
	'add data from file
	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
	'bind data
	With DataGridView1
		.AutoGenerateColumns = True
		.DataSource = dt
	End With
End Sub


Linq solution:
VB
Private Sub BindData(sFileName As String)

	Dim lines As String() = File.ReadAllLines(sFileName)
	'create a datatable object to bind data to DataGridView component
Dim dt As DataTable = New DataTable()
	'add columns 
	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")) _
		})
	'add data from file
	dt = lines _
		.Select(Function(x) dt.LoadDataRow(x.Split(New String(){vbTab}, StringSplitOptions.RemoveEmptyEntries), False)) _
		.CopyToDataTable()
	'bind data
	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!
 
Share this answer
 
thanks for help
i have tried yesterday and it work however it does fill my goal
it show the data but after last rows from textfile and not from first row

dt.Columns.Add("Command")
       dt.Columns.Add("Article")
       dt.Columns.Add("Qte")
       dt.Columns.Add("PU")

       Dim ouvrir As New OpenFileDialog
       If ouvrir.ShowDialog = DialogResult.OK Then
           Dim sr As New StreamReader(ouvrir.FileName)
           While Not sr.EndOfStream
               dt.Rows.Add(sr.ReadLine.Split(CChar(vbTab)))
           End While
           Using cmd As New SqlCommand("select Designation from DetailReceptionFrs", cn)
               Dim da As SqlDataReader = cmd.ExecuteReader

               dt.Columns.Add("Designation")
               While da.Read
                   Dim newRows As DataRow = dt.NewRow
                   newRows("Designation") = da("Designation")
                   dt.Rows.Add(newRows)
               End While
           End Using

           DataGridView1.DataSource = dt
       End If
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900