T
teddysnips
WINDOWS FORM>
We are migrating an app from VS2003 using DataGrid to VS2005 using
DataGridView. Naturally, all our DataGrid helper classes are now
redundant. This is more or less my first exposure to this technology
- I've spent the past five years on ASP.NET
I want to create a very simple builder form (one that allows the user
to modify tables that hold look-up data for the main application - for
example, my current builder is "Location Type", and contains values
such as "School", "Post Office", "Community Hall" etc.)
The new, VS2k5 form is required to have the same look and feel as the
previous version. It's got "New", "Save" and "Delete" buttons.
The form has a BindingSource object and this is set to a DataTable at
runtime by executing a stored procedure:
Me.bsLocationType.DataSource = GetGridData()
......
Public Function GetGridData() As DataTable
' Get dataview based on criteria
Dim ds As DataSet =
SqlHelper.ExecuteDataset(DBConnection.ConnectionString,
CommandType.StoredProcedure, "stpGetLocationType")
' Return data
Return ds.Tables(0)
End Function
......
The SQL in the SPROC is:
SELECT
LocTypeID,
LocTypeCode AS [Type Code],
LocTypeDescription AS [Location Description]
FROM
tblLocationType
ORDER BY
LocTypeDescription
......
If the user presses "New" I can persuade the DataGridView to add a new
row, and move the focus to that row (I do this by adding a row to the
underlying DataTable) though I'm struggling to get the focus to the
first editable cell in the row.
Dim dt As DataTable = Me.bsLocationType.DataSource
Dim row As DataRow = dt.NewRow()
dt.Rows.Add(row)
Dim rowIndex As Integer = Math.Max(0, bsLocationType.Count)
dgRecords.CurrentCell = dgRecords.Rows(rowIndex - 1).Cells(1)
The user can then enter data to the new row.
BUT.
BUT.
No matter what I try, I can't seem to get the data to save! I'm sure
I could roll my own function - something along the lines of either a)
generating some in-line SQL - e.g.
Dim strSQL As String = "INSERT INTO LocationType (Code, Description)
VALUES (" & dgRecords.Rows(rowIndex - 1).Cells(1).Value & ", " &
dgRecords.Rows(rowIndex - 1).Cells(2).Value & ")"
or b) using the row values as parameters to an INSERT stored
procedure.
and run either of these against the database. Bit clunky though, and
sort of takes away the point of DataBinding, to me at any rate. If
the user tabs or moves away from the "new" row the data persists in
the grid, but if I close the form, the data disappears. In other
words, the DataTable to which the DataViewGrid has the data, but it's
not being written back to the Database.
Thoughts?
Edward
We are migrating an app from VS2003 using DataGrid to VS2005 using
DataGridView. Naturally, all our DataGrid helper classes are now
redundant. This is more or less my first exposure to this technology
- I've spent the past five years on ASP.NET
I want to create a very simple builder form (one that allows the user
to modify tables that hold look-up data for the main application - for
example, my current builder is "Location Type", and contains values
such as "School", "Post Office", "Community Hall" etc.)
The new, VS2k5 form is required to have the same look and feel as the
previous version. It's got "New", "Save" and "Delete" buttons.
The form has a BindingSource object and this is set to a DataTable at
runtime by executing a stored procedure:
Me.bsLocationType.DataSource = GetGridData()
......
Public Function GetGridData() As DataTable
' Get dataview based on criteria
Dim ds As DataSet =
SqlHelper.ExecuteDataset(DBConnection.ConnectionString,
CommandType.StoredProcedure, "stpGetLocationType")
' Return data
Return ds.Tables(0)
End Function
......
The SQL in the SPROC is:
SELECT
LocTypeID,
LocTypeCode AS [Type Code],
LocTypeDescription AS [Location Description]
FROM
tblLocationType
ORDER BY
LocTypeDescription
......
If the user presses "New" I can persuade the DataGridView to add a new
row, and move the focus to that row (I do this by adding a row to the
underlying DataTable) though I'm struggling to get the focus to the
first editable cell in the row.
Dim dt As DataTable = Me.bsLocationType.DataSource
Dim row As DataRow = dt.NewRow()
dt.Rows.Add(row)
Dim rowIndex As Integer = Math.Max(0, bsLocationType.Count)
dgRecords.CurrentCell = dgRecords.Rows(rowIndex - 1).Cells(1)
The user can then enter data to the new row.
BUT.
BUT.
No matter what I try, I can't seem to get the data to save! I'm sure
I could roll my own function - something along the lines of either a)
generating some in-line SQL - e.g.
Dim strSQL As String = "INSERT INTO LocationType (Code, Description)
VALUES (" & dgRecords.Rows(rowIndex - 1).Cells(1).Value & ", " &
dgRecords.Rows(rowIndex - 1).Cells(2).Value & ")"
or b) using the row values as parameters to an INSERT stored
procedure.
and run either of these against the database. Bit clunky though, and
sort of takes away the point of DataBinding, to me at any rate. If
the user tabs or moves away from the "new" row the data persists in
the grid, but if I close the form, the data disappears. In other
words, the DataTable to which the DataViewGrid has the data, but it's
not being written back to the Database.
Thoughts?
Edward