DotNetNewsgroup.com  
web access to complete list of Microsoft.NET newsgroups
   home   |   control panel login   |   archive  |  
 
  carried group
academic
adonet
aspnet
aspnet.announcements
aspnet.buildingcontrols
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
assignment_manager
datatools
dotnet.distributed_apps
dotnet.general
dotnet.myservices
dotnet.nternationalization
dotnet.scripting
dotnet.security
dotnet.vjsharp
dotnet.vsa
dotnet.xml
dotnetfaqs
framework
framework.clr
framework.compactframework
framework.component_services
framework.controls
framework.databinding
framework.drawing
framework.enhancements
framework.interop
framework.odbcnet
framework.performance
framework.remoting
framework.sdk
framework.setup
framework.webservices
framework.windowsforms
framework.wmi
frwk.windowsforms.designtime
lang.csharp
lang.jscript
lang.vb
lang.vb.controls
lang.vb.data
lang.vb.upgrade
lang.vc
lang.vc.libraries
  
 
start date: Tue, 21 Aug 2007 07:32:55 -0700,    posted on: microsoft.public.dotnet.framework.windowsforms.databinding        back       

Thread Index
  1    unknown


Can add new data to DataGridView but can't save it!   
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
Date:Tue, 21 Aug 2007 07:32:55 -0700   Author:  

Google
 
Web dotnetnewsgroup.com


COPYRIGHT ?2005, EUROFRONT WORLDWIDE LTD., ALL RIGHT RESERVE  |   Contact us