all groups > dotnet windows forms databinding > august 2007 >
You're in the

dotnet windows forms databinding

group:

Can add new data to DataGridView but can't save it!


Can add new data to DataGridView but can't save it! teddysnips NO[at]SPAM hotmail.com
8/21/2007 7:32:55 AM
dotnet windows forms databinding:
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
Re: Can add new data to DataGridView but can't save it! Cor Ligthert[MVP]
8/22/2007 12:00:00 AM
Teddy,

The windowform datagrid (as well as the aspnet datagrid) are still in
VS2005, it is not direct in the toolbox, however, with not much more than a
simple click in the dialog to add, will add it to that toolbox.

The change that it ever disappears from dotNet is in my idea low, breaking
changes of the existing code is a very important isue not to be done for the
VB develloping team.

Cor

<teddysnips@hotmail.com> schreef in bericht
news:1187706775.835678.37170@19g2000hsx.googlegroups.com...
[quoted text, click to view]
Re: Can add new data to DataGridView but can't save it! teddysnips NO[at]SPAM hotmail.com
8/22/2007 1:23:48 AM
On Aug 22, 5:30 am, "Cor Ligthert[MVP]" <notmyfirstn...@planet.nl>
[quoted text, click to view]

Thank you for taking the time to respond. However, you are responding
to a question that I didn't ask - the decision to replace the DataGrid
with the DataGridView has already been taken. There is a very good
reason for this - in terms of our application the DataGrid has a
killer bug that has never been resolved.

So, once again, how do I do the save on the DataGridView please?

Edward

AddThis Social Bookmark Button