passing datasets between forms

Posted on 2007-03-23
Medium Priority
Last Modified: 2013-12-17
I have a dataset that is bound to a grid.  When you double click on a row in the grid, an edit screen pops up where you can add or edit records.  I pass the dataset by reference.  If I instead pop up the same screen with an add button, I add a row to the dataset, make my changes, then save.  The problem is that if I add a row, even when I get to the add/edit screen, the grid on the main form will show a blank line.  It is still bound to the dataset that was passed in (by val, which didn't really work).

The problem is that my "helper" form can add/edit the dataset, but it is affecting the main "browse" form.    I don't think that it is a good idea to have forms effect other forms data.  How are you supposed to do this?  Should I make a copy of the dataset, change that, then merge when I get back to the main page?  create a new row, only add to the ds when I save?  If I do, what happens when they violate some dataset constraints?

What are my options, and what is the best way to do this?  I don't want to have go to the database every time I pop up a screen.  I would rather edit the ds that I have, but I dont' want it to effect grids.
Question by:jackjohnson44
LVL 25

Expert Comment

ID: 18780560
Windows or web?
LVL 18

Expert Comment

by:Ajay Sharma
ID: 18784700
if it is a web app :
then before redirect the page, store the dataset into a session and get the session value on to the next page. In page1.aspx:

Session("storedata") = datasetobject

in page2.aspx form_load

dim ds as dataset = Session("storedata")


if it is a windows app :
create a module called module1.vb

Public ds as dataset

then in the first from

in the second form


Author Comment

ID: 18785255
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 29

Expert Comment

by:Gautham Janardhan
ID: 18785526
try passing


and then merging the two datasets' after the opertion..
LVL 34

Accepted Solution

Sancler earned 2000 total points
ID: 18786526
I don't think that it is a good idea to have forms effect other forms data.

I'd want some more detailed explanation of the context before being sure, but in terms of general principle I disagree.  

In my view, data is an Application's data, rather than a Form's data.  Different Forms may look at that data in different ways and facilitate different operations on it.  But I wouldn't want different Forms in an Application of mine to be working with different data.

But it's your Application ;-)

I think my approach to this would be to use a single central dataset/datatable but to interpose a dataview between that and the datagrid/s that I did not want to alter when new records were added.  The details would depend on the datatable structure/contents but, for instance, when a record is added to a datatable in the Application, it will often not have anything in its Primary Key field: that will often be allocated by the database when the record is saved.  So a dataview filter excluding records having Null in the Primary Key field would stop "new" records being shown.  Or dataviews can be filtered by RowState.  So a dataview with its .RowStateFilter set to ModifiedOriginal would stop "new" records being shown.


Author Comment

ID: 18786600
thanks for taking a look at this.  I really don't have a preference as to how the datasets are shared/passed, I would like to know what is considered best before I try to try new things.  I am using a guid for a primary key and create on in the form and set the cell to that.  I understand your approach to filtering on a null value, but because it is strongly typed, I have to put that in there.  How would you suggest filtering the records to not show new rows, only updates?

Another thing to consider:

Also, if I try allow the browse grid on the main form to be editable/add row enabled too, as soon as I started typing in a column, my criteria for filter would probably not be met and it would disappear from the grid.
LVL 34

Expert Comment

ID: 18787761
Try this demo.  A form with one datagridview - called dgv - and this code

Public Class Form1

    Private dt As New DataTable("TestTable")
    Private dv As New DataView

    Private Sub filltable()
        Dim dc As New DataColumn("ID", GetType(Integer))
        dc.AutoIncrement = True
        Dim dc1 As New DataColumn("Name", GetType(String))
        For i As Integer = 1 To 10
            Dim dr As DataRow = dt.NewRow
            dr(1) = "Name" & i.ToString
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        dv.Table = dt
        dv.RowStateFilter = DataViewRowState.Unchanged Or DataViewRowState.ModifiedCurrent
        dgv1.DataSource = dv
    End Sub

End Class

The first sub is just to provide dummy data on which to work.  It is the stuff in the form load sub that might interest you.

The .AllowUserToAddRows property is left as True.  So it is possible to enter a new row but, when and only when that new record is "committed" to the datatable - e.g. by the user clicking on a different row - it disappears from the grid.  That is, the dataview's filter only operates on "committed" records.  I would have thought, though, that that was unlikely to be a real problem.  If your users are supposed to enter new records on a different form, I would have thought it sensible to set .AllowUserToAddRows to false on your "browsing" grid.


Author Comment

ID: 18788722
thanks Roger,
I will try that.

My browse grid did not allow users to add rows.  When I popped up my add/edit screen, I either had a either bound to the row being edited via a currency manager, or added a new row to the dataset and bound to it.  When I add this row, the screen in the background, the browse screen, will display the blank row as soon as my add/edit screen adds the row.  That is pretty much the only behavior that I want to get rid of.  I would like to have to "accept" in some way on the add/edit screen before any other screens see this new, or edited data.  Also, if someone edits a name for instance, and the add/edit screen crashes, the browse screen could show data that was never verified or really saved.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question