in my invoicing software I have the "master" datagridview which I integrated with a lookup feature: once user presses F3 while on the Code field, it shows a modal form with another datagridview that shows a list of products to choose from.
Where i'm stuck is where user clicks Ok on the selected products, because I don't know well how to fill the Datagridview correctly in order to have all on the same row. Here's my code:
Private Function RicercaxCodiceArticolo(ByVal codiceart As String)
Dim strsql As String
Dim cmd As SqlCommand
Dim source As New BindingSource
Dim count As Integer
connection.Open()
strsql = "SELECT CODICEARTICOLO AS 'Codice', DESCRIZIONEARTICOLO AS 'Descrizione', UNITAMISURA AS 'Um', CODICEIVA AS 'Iva' " _
& ",COSTOBASE AS 'Costo', PREZZOBASE AS 'Prezzo', COSTOULTIMO AS 'Costo Ult.', BARCODE AS 'Barcode', NOTEARTICOLO AS 'Note' " _
& ",CATEGORIAARTICOLO AS 'Categ.Art.', GIACENZA AS 'Giacenza', FORNITOREPREF AS 'Fornit. Pref.' FROM Articoli " _
& " WHERE CODICEARTICOLO = '" & codiceart & "'"
cmd = New SqlCommand()
cmd.CommandText = strsql
cmd.CommandType = CommandType.Text
cmd.Connection = connection
'TODO: Completare
source.DataSource = cmd.ExecuteReader
DataGridView1.Rows.Add(source.Current!Codice)
DataGridView1.Rows.Add(source.Current!Descrizione)
connection.Close()
End Function
Of course the second line of the Add method is wrong, but how do I fill all the data on the same line?
CodePudding user response:
Well … I will assume a couple of things that you are neglecting to tell us.
We must assume that the first grid does NOT use a
DataSource
and the columns are added in the designer or in some code you are not showing. This assumption is based on the code that adds the rows directly to the grid since you can not add rows directly to the grid when it is data bound.Also, I will assume that the line of code …
source.DataSource = cmd.ExecuteReader
… is returning a
DataTable
from the SQL commandcmd
.
If both my assumptions are correct, then in my small tests, the line of code…
source.Current
Will return a DataRowView
object of the “selected” row in source
and you simply need to cast it to a DataRowView
to get the values you want to add to the single row in the grid. Something like…
Dim drv As DataRowView
drv = CType(source.Current, DataRowView)
DataGridView1.Rows.Add(drv("Codice"), drv("Descrizione"))
Lastly, it may be a better approach to use a DataTable
as a DataSource
for the first grid's BindingSource
and simply "Import" the row into the DataTable
. Pick your own poison.