dcsimg
Results 1 to 27 of 27

Thread: [RESOLVED] Add a new Column to a DataGridView control

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    61

    Resolved [RESOLVED] Add a new Column to a DataGridView control

    I have a DataGridView with DataSource from a text file

    Code:
    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
    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 automatcally and what I want to add it as new column in the middle of the DataGridView.

    My code its just:

    Code:
    Dim cmd As New SqlCommand("select numero from database", cn)  
    Dim da As New SqlDataAdapter(cmd)  
    Dim dt2 As New DataTable  
    da.Fill(dt2)  
    datagridview1.DataSource = dt2
    Please i am stuck

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,240

    Re: Add a new Column to a DataGridView control

    You can't add rows to the grid manually and by binding as well. If you want to set the DataSource, you can't Add to the Rows collection.

    Start by adding all the columns to the grid, either in the designer or in code. For the column that you want to bind to the database data, set the DataPropertyName property of the column to the name of the data source column, which appears to be "numero". Get the data from that database first and set the DataSource, which will add all the rows to the grid. You can then populate the cells of those rows from the text file.

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    61

    Re: Add a new Column to a DataGridView control

    i can't bind datasource with my database because i have error that says sqlserver that i use is not supported its sqlserver2000
    its more easier to use the bindingsource
    Last edited by houssem; Apr 16th, 2019 at 06:04 AM.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,240

    Re: Add a new Column to a DataGridView control

    You can install SQL Server LocalDB as part of VS or you can download and install SQL Server Express so compatibility is no issue.

    A BindingSource isn't really relevant. Yes, you can use one when you bind data and I would generally recommend that you do but you have to have a populated DataTable to begin with regardless.

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    61

    Re: Add a new Column to a DataGridView control

    i can't install other sqlserver because this our envirement work so i have use sql server 2000
    i can work perfectly waith it but only visual studio can't recognise it.
    now i just need to add that column i looking for,and i have a lot of other work to do please help me,

  6. #6
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,086

    Re: Add a new Column to a DataGridView control

    how does tha Data relate to each other ? I mean numero to the Data in the Textfile

    what you could do is..
    export the Sql Statement to a text file
    then merge the data from both textfiles
    then you only have to read the 'merged' Textfile to the DGV


    EDIT:
    this is what File1 looks like
    Code:
    Col1,col2
    as,dd
    vv,mm
    this is what File2 looks like
    Code:
    Col3,col4
    as2,dd2
    v2v,mm2
    I used Powershell to merge the Data
    Code:
    $txt1 = Get-Content 'E:\File1.txt'
    $txt2 = Get-Content 'E:\File2.txt'
    
    0..($txt2.Count – 1) | %{$txt1[$_],$txt2[$_] -join ','} | Out-File 'E:\File1and2.txt'
    the new File1and2.txt looks like this
    Code:
    Col1,col2,Col3,col4
    as,dd,as2,dd2
    vv,mm,v2v,mm2
    Last edited by ChrisE; Apr 16th, 2019 at 12:37 PM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    61

    Re: Add a new Column to a DataGridView control

    thanks for your help but my problem is:
    1-i have to read a text file to datagridview{solved} its 4 columns Command+Article+Qte+PU
    Command Article Qte PU
    abc123456 175c 5 1.2

    2-i have to read from database{solved} its just 1 column Designation
    Designation
    abcdef
    3-from above i should have a datagridview with 5 columns {not yet and i am looking for solution} Commande+Article+Qte+PU+Designation
    Commande Article Qte PU Designation
    abc123456 175c 5 1.2 abcdef

    perhaps this clarify everithing

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    61

    Re: Add a new Column to a DataGridView control

    please help i still need to resolve it

  9. #9
    Lively Member
    Join Date
    Jul 2017
    Location
    Exeter, UK
    Posts
    105

    Re: Add a new Column to a DataGridView control

    I don't understand why you can't simply, first add your columns to the DGV, then populate them from reading in the file and then from the database. You do not need to use any data binding. I am assuming you will know the relationship between the DGV row and your database column.

  10. #10
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,013

    Re: Add a new Column to a DataGridView control

    I been down this road with him a few times by now, your all wasting your time.

  11. #11

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    61

    Re: Add a new Column to a DataGridView control

    i did everything without a solution its look easy there is my try
    Code:
      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
    it add the column i want but it begin from last rows from textfile not in first rows Name:  Sans titre2.jpg
Views: 49
Size:  29.2 KB

  12. #12
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,191

    Re: Add a new Column to a DataGridView control

    You don't want to add more rows. You want to edit the rows you already have to change the information in the column you added. A generic syntax for doing that assuming you are editing the DGV rather than DataTable:
    Code:
    dataGridView.Rows(row_number).Cells(column_number).Value = new_value
    column_number could also be exchanged for "column_name"

  13. #13

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    61

    Re: Add a new Column to a DataGridView control

    still the same result as before

  14. #14
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,013

    Re: Add a new Column to a DataGridView control

    still the same result as before
    As far as I am concerned you are just waiting for someone to plop an copy/paste solution for you. You are not learning anything, you are not doing any research. I personally have shown you several methods in which you can perform this operation, and you have failed to produce any kind of meaningful replies, such as exceptions you may have received, simple answers to questions such as how are the 'tables' even related.

  15. #15

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    61

    Re: Add a new Column to a DataGridView control

    no way to copy/paste i work hard to find a solution don't take any prejudice
    and its not a hall code its just simple line that make everything in place
    my app is just few step to finish
    so for the table i use one table thats all
    and i think that it should a command or line of code that make the rows rise up
    my last try
    Code:
      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
    
    
                customersDataGridView.DataSource = dt
            End If
            cn.Open()
    
            Using cmd As New SqlCommand("select Designation from DetailReceptionFrs", cn)
                Dim da As SqlDataReader = cmd.ExecuteReader
    
                dt.Columns.Add("Designation")
    
                While da.Read
    
    
                    dt.Rows(0)("Designation") = da("Designation")
    
                End While
                customersDataGridView.Rows(0).Cells("Designation").Value = dt
    
            End Using

  16. #16
    Lively Member
    Join Date
    Jul 2017
    Location
    Exeter, UK
    Posts
    105

    Re: Add a new Column to a DataGridView control

    You code is doing exactly as its written.

    You are assuming a 1 to 1 relation i.e. the same number of rows in the text file and what's retrieved from the database.

    Code:
    dt.Columns.Add("Designation")
    While da.Read
         Dim newRows As DataRow = dt.NewRow  ' This is wrong and is not needed, think about it, your adding NEW rows to the table.
         newRows("Designation") = da("Designation") This is wrong, you should be setting the column value for the row no being read. 
         dt.Rows.Add(newRows) ' This is wrong, it is not needed.
    End While

  17. #17
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,191

    Re: Add a new Column to a DataGridView control

    Quote Originally Posted by houssem View Post
    no way to copy/paste
    ??? You copied and pasted some code in post #1 and #11. Yet you didn't do it again in #13 when you obviously did something wrong or you could not have gotten the same result.

  18. #18

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    61

    Re: Add a new Column to a DataGridView control

    please revirefy all code made by myself and i builded step by step
    from where i copied give me what you say about

  19. #19

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    61

    Re: Add a new Column to a DataGridView control

    please revirefy all code made by myself and i builded step by step
    from where i copied give me what you say about

  20. #20
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,191

    Re: Add a new Column to a DataGridView control

    You need to step away from the keyboard for a while. As our most prolific member here would say (my paraphrase), you need to write out all the steps on how you would solve this problem as if you had to do it by hand. Only after you have a firm grasp on the steps needed to solve the issue should you try to write code to do each step.

    You didn't research at all what I told you to do. What do you think this line will do?
    Code:
    customersDataGridView.Rows(0).Cells("Designation").Value = dt

  21. #21

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    61

    Re: Add a new Column to a DataGridView control

    when i execute show interrogation point
    Name:  Sans titre4.jpg
Views: 43
Size:  30.2 KB
    Code:
    Using cmd As New SqlCommand("select Designation from DetailReceptionFrs", cn)
                Dim da As SqlDataReader = cmd.ExecuteReader
                While da.Read
                    customersDataGridView.Rows(0).Cells("Designation").Value = da("Designation")
                End While
            End Using
    Last edited by houssem; Apr 18th, 2019 at 10:48 AM.

  22. #22
    Lively Member
    Join Date
    Jul 2017
    Location
    Exeter, UK
    Posts
    105

    Re: Add a new Column to a DataGridView control

    Code:
    Using cmd As New SqlCommand("select Designation from DetailReceptionFrs", cn)
         Dim da As SqlDataReader = cmd.ExecuteReader
         dim rowcount as integer = 0
         While da.Read
                    customersDataGridView.Rows(rowcount).Cells("Designation").Value = da("Designation")
                    rowcount+=1
         End While
    End Using

  23. #23
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,191

    Re: Add a new Column to a DataGridView control

    Quote Originally Posted by houssem View Post
    when i execute show interrogation point
    If you are going to continue to insert pictures, crop them first so only the relevant portion is showing. Otherwise, they are usually too small to be useful. The shortcut to get just the active window (form) is Alt+PrtScr.

  24. #24

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    61

    Re: Add a new Column to a DataGridView control

    Quote Originally Posted by bmwpete View Post
    Code:
    Using cmd As New SqlCommand("select Designation from DetailReceptionFrs", cn)
         Dim da As SqlDataReader = cmd.ExecuteReader
         dim rowcount as integer = 0
         While da.Read
                    customersDataGridView.Rows(rowcount).Cells("Designation").Value = da("Designation")
                    rowcount+=1
         End While
    End Using
    i thaught of that before and test it always interogation point in place but from fisrt row

  25. #25
    Lively Member
    Join Date
    Jul 2017
    Location
    Exeter, UK
    Posts
    105

    Re: Add a new Column to a DataGridView control

    I don't understand any of that.

    How many rows of data are there in the text file? Do you expect to get a "Designation" value from the datatable for each row in the text file?

  26. #26
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,013

    Re: Add a new Column to a DataGridView control

    Ok, for the the sake of getting you past this and moving along I got you to as close of a copy/paste solution as possible. You may have to apply just a bit of common sense, such as understanding where column names should be replaced.

    Code:
    Imports System.Data.SqlClient
    
    Public Class FormHous
        Private Sub FormHous_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'Stop adding rows to the datagridview!
            'Use a Datatable (for the 5th time!)
    
            Dim DT As New DataTable With {.TableName = "hous"}
            DT.Columns.Add("Col1", GetType(String)) 'txt column
            DT.Columns.Add("Col2", GetType(String)) 'txt column
            DT.Columns.Add("Col3", GetType(String)) 'txt column
            DT.Columns.Add("Col4", GetType(String)) 'txt column
            DT.Columns.Add("Col5", GetType(String)) 'txt column
    
            DT.Columns.Add("Col6", GetType(String)) 'sql column
    
            Dim sr As New IO.StreamReader(ouvrir.FileName)
    
            Using cn As New SqlConnection("Your COnn String")
                cn.Open()
    
                Using EXTReader As SqlDataReader = New SqlCommand("select numero from database", cn).ExecuteReader
                    While sr.Peek <> -1
                        EXTReader.Read()
                        Dim TxtVals() As String = sr.ReadLine.Split(CChar(vbTab))
                        Dim Nrow As DataRow = DT.NewRow
                        Nrow("Col1") = TxtVals(0)
                        Nrow("Col2") = TxtVals(1)
                        Nrow("Col3") = TxtVals(2)
                        Nrow("Col4") = TxtVals(3)
                        Nrow("Col5") = TxtVals(4)
    
                        Nrow("Col6") = EXTReader.GetString(EXTReader.GetOrdinal("ColumnName")) 'Column from SQL
    
                        DT.Rows.Add(Nrow)
                    End While
                End Using
                cn.Close()
            End Using
    
            DataGridView1.DataSource = DT
        End Sub
    End Class

  27. #27

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    61

    Re: Add a new Column to a DataGridView control

    Quote Originally Posted by kpmc View Post
    Ok, for the the sake of getting you past this and moving along I got you to as close of a copy/paste solution as possible. You may have to apply just a bit of common sense, such as understanding where column names should be replaced.

    Code:
    Imports System.Data.SqlClient
    
    Public Class FormHous
        Private Sub FormHous_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'Stop adding rows to the datagridview!
            'Use a Datatable (for the 5th time!)
    
            Dim DT As New DataTable With {.TableName = "hous"}
            DT.Columns.Add("Col1", GetType(String)) 'txt column
            DT.Columns.Add("Col2", GetType(String)) 'txt column
            DT.Columns.Add("Col3", GetType(String)) 'txt column
            DT.Columns.Add("Col4", GetType(String)) 'txt column
            DT.Columns.Add("Col5", GetType(String)) 'txt column
    
            DT.Columns.Add("Col6", GetType(String)) 'sql column
    
            Dim sr As New IO.StreamReader(ouvrir.FileName)
    
            Using cn As New SqlConnection("Your COnn String")
                cn.Open()
    
                Using EXTReader As SqlDataReader = New SqlCommand("select numero from database", cn).ExecuteReader
                    While sr.Peek <> -1
                        EXTReader.Read()
                        Dim TxtVals() As String = sr.ReadLine.Split(CChar(vbTab))
                        Dim Nrow As DataRow = DT.NewRow
                        Nrow("Col1") = TxtVals(0)
                        Nrow("Col2") = TxtVals(1)
                        Nrow("Col3") = TxtVals(2)
                        Nrow("Col4") = TxtVals(3)
                        Nrow("Col5") = TxtVals(4)
    
                        Nrow("Col6") = EXTReader.GetString(EXTReader.GetOrdinal("ColumnName")) 'Column from SQL
    
                        DT.Rows.Add(Nrow)
                    End While
                End Using
                cn.Close()
            End Using
    
            DataGridView1.DataSource = DT
        End Sub
    End Class
    thank you for helping me
    Last edited by houssem; Apr 19th, 2019 at 03:25 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width