Results 1 to 9 of 9

Thread: insert row in datagridview

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2022
    Posts
    8

    insert row in datagridview

    i have a vb.net app with a datagridview that is filled by a view with an underlying table.
    now i want to insert rows in the datagridview like in excel (under the selected row).
    i have been working with chatgpt for a few days and currently have the following code.
    Code:
      Private Sub BtnNewP_Click(sender As Object, e As EventArgs) Handles BtnNewP.Click
            Try
                ' Controleer of er een rij in de DataGridView is geselecteerd
                If ParticulierDataGridView.CurrentRow Is Nothing Then
                    MessageBox.Show("Selecteer een rij in de view om 'Merk' van over te nemen.")
                    Exit Sub
                End If
    
                ' Haal de geselecteerde DataRowView op
                Dim drv As DataRowView = CType(ParticulierDataGridView.CurrentRow.DataBoundItem, DataRowView)
                Dim merkVal As Object = drv("Merk")
    
                ' Controleer of "Merk" niet leeg is
                If IsDBNull(merkVal) OrElse merkVal.ToString().Trim() = "" Then
                    merkVal = "GeenMerk" ' Of laat dit leeg, afhankelijk van je kolomvereisten
                End If
    
                ' Nu doe je een Insert in de TABEL "Cataloog" via de CataloogTableAdapter
                ' Pas onderstaande Insert-call aan op jouw kolommen en methodesignature.
                ' Hier vullen we alleen "Merk" en geven we default of lege waarden aan de rest.
    
                Dim rowsAffected As Integer = Me.CataloogTableAdapter.Insert(
                    merkVal.ToString(),     ' Merk
                    "",                    ' ref_vast
                    "",                    ' ref_es
                    "",                    ' Ref
                    "",                    ' Omschrijving
                    "",                    ' Datum
                    "",                    ' Snijden
                    "",                    ' Bol
                    "",                    ' ref_set7P
                    "",                    ' ref_set13P
                    0D,                    ' werk_garage
                    0D,                    ' werk_particulier
                    0D,                    ' werk_es
                    0D,                    ' opleg_13P
                    0D,                    ' prijs_vast
                    0D,                    ' prijs_es
                    0D,                    ' prijs_set7P
                    0D,                    ' prijs_set13P
                    "",                    ' Codering
                    0D,                    ' Codering_prijs
                    "",                    ' Spoiler
                    0D,                    ' Spoiler_prijs
                    "",                    ' Uitbreidingsset
                    0D,                    ' Uitbreiding_prijs
                    0D,                    ' Caravan
                    ""                     ' PDC
                )
    
                MessageBox.Show("Rijen toegevoegd in de tabel Cataloog: " & rowsAffected.ToString())
    
                ' (Optioneel) Als je wilt dat de nieuwe rij ook in de view zichtbaar wordt,
                ' herlaad dan de DataTable "Particulier". Dit werkt alleen als de view 
                ' die nieuwe rij ook toont.
                Me.DataSet.Particulier.Clear()
                Me.ParticulierTableAdapter.Fill(Me.DataSet.Particulier)
    
            Catch ex As Exception
                MessageBox.Show("Fout bij invoegen in Cataloog: " & ex.Message)
            End Try
        End Sub
    this inserts the row but due to the autonumbering it ends up at the bottom and not under the selected row.
    anyone an idea if this is possible or a different/better approach.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,076

    Re: insert row in datagridview

    If your grid is bound then it will display whatever's in the data source. If that data source is a DataView then, in order for the grid to display rows in a certain order, the DataView would have to be sorted such that its rows were in that order. If you have no existing column to sort by to achieve that, you can add a column to the DataTable for display order, then sort the DataView by that column. If you want the data sorted by some other column at the start, sort by the other column first, then populate the display order column sequentially, then sort by the display order column. When you add a new row, you simply set the display order field to a value that is between that for the two rows you want the new row to appear between. You may need to increment all the rows below it if you want to keep the values sequential. Note that you would probably not want a visible column in the grid for that column in the data source.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2022
    Posts
    8

    Re: insert row in datagridview

    I want this to happen automatically without having to manually number each row. like you can add rows in excel.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,076

    Re: insert row in datagridview

    Oh, I didn't realise that you wanted something. OK, then. I guess you should just wave your magic wand and have it work just how you want.

    Seriously though, a DataGridView is not Excel. It works how it works. What the user will see is just like it works in Excel but you, the developer, have to actually use the tools at your disposal to make that happen in code. I've told you what to do. You can either do it or wish you didn't have to. It's up to you.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2022
    Posts
    8

    Re: insert row in datagridview

    no need to be sarcastic. my question is simply whether i have to manually number each row or if there is a way to automatically adjust the numbering when entering a row between 2 existing rows. i am not a professional programmer.

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,382

    Re: insert row in datagridview

    Quote Originally Posted by mustangBE View Post
    no need to be sarcastic. my question is simply whether i have to manually number each row or if there is a way to automatically adjust the numbering when entering a row between 2 existing rows. i am not a professional programmer.
    Simple answer is NO.

    Perhaps if you explain why you need to insert the new row instead of adding it to the end, maybe there is a simple solution. Maybe don't bind the DGV or use a different field for the sort order. But we need more information.

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2022
    Posts
    8

    Re: insert row in datagridview

    it is a catalog with car models to look up prices of parts. between the parts i want to insert an empty row to make groups or add a new article.

  8. #8
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,382

    Re: insert row in datagridview

    Are you planning to put empty rows in your database? Not a good idea usually.

    It sounds like you could sort on car model + autonumber. If not you might have to forget about binding and load the DGV manually.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,076

    Re: insert row in datagridview

    Quote Originally Posted by mustangBE View Post
    no need to be sarcastic.
    No, but there was a desire and, in my opinion, a justification.
    Quote Originally Posted by mustangBE View Post
    my question is simply whether i have to manually number each row or if there is a way to automatically adjust the numbering when entering a row between 2 existing rows. i am not a professional programmer.
    What did I actually say?
    You may need to increment all the rows below it if you want to keep the values sequential.
    If you don't care about sequential values then all you have to do is number the new row with a value that is between the values for the rows on either side of it. If you use Double values instead of Integer then you could start with 0.0, 1.0, 2.0, 3.0, etc, then you can add a row with the value 1.5 to insert it between the second and third existing rows. You can then use 1.25 or 1.75 to put a row on either side of that, etc. You don't need to be a programmer to understand the logic because it's simple numbers. The actual values don't matter because they are being used solely to sort at run time and are never saved. Only their relative values matter.

    That said, renumbering the existing rows is pretty simple stuff anyway. If you started with 0, 1, 2, 3, etc, and you want to insert a new row after the current second row, you would simply get the rows with value >= 2, loop over them backwards and increment the value each time. That would not affect the ordering but it would leave a gap, which you could then fill with a new row.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width