-
Mar 15th, 2025, 02:39 PM
#1
Thread Starter
New Member
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.
-
Mar 15th, 2025, 11:18 PM
#2
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.
-
Mar 16th, 2025, 05:16 AM
#3
Thread Starter
New Member
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.
-
Mar 16th, 2025, 10:40 AM
#4
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.
-
Mar 16th, 2025, 01:59 PM
#5
Thread Starter
New Member
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.
-
Mar 16th, 2025, 03:04 PM
#6
Re: insert row in datagridview
 Originally Posted by mustangBE
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.
-
Mar 16th, 2025, 03:48 PM
#7
Thread Starter
New Member
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.
-
Mar 16th, 2025, 04:16 PM
#8
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.
-
Mar 17th, 2025, 01:47 AM
#9
Re: insert row in datagridview
 Originally Posted by mustangBE
no need to be sarcastic.
No, but there was a desire and, in my opinion, a justification.
 Originally Posted by mustangBE
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|