I have a DataGridView with 5 columns all setup in the Form Designer. I also have a Stored Procedure that will SELECT
the 5 columns and data required based on parameters. I want this data to be updateable; the user can add rows, delete rows and update existing rows.
I have so far managed to populate the DataGridView with data from a Stored Procedure in SQL Server, but unfortunately this is read-only and any changes made to the datatable post-load are only in the front end application.
I have done this many times in MS Access and I believe the DataSet
needs to be 'bound' to the DataGridView rather than fed as a DataSource
in order to make the DataGridView updateable directly to the data table in SQL Server but I am not sure on how to achieve this. I have looked online at very many sources and cannot seem to work out what it is I need to do in order to get this working.
Existing code:
Public Sub loadDataGridView(taskID As Integer)
Dim db As New SqlConnection(getCredentials)
Dim sp As SqlCommand = db.CreateCommand
Dim sa As SqlDataAdapter = New SqlDataAdapter(sp)
Dim ds As DataSet = New DataSet()
Dim x As Integer = 0
db.Open()
With sp
.CommandText = "StoredProcedureName"
.CommandType = CommandType.StoredProcedure
.Parameters.Add(New SqlParameter("@taskID", taskID))
End With
sa.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
db.Close()
End Sub