passing datasets between forms

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Windows or web?
Ajay SharmaCommented:
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

jackjohnson44Author Commented:
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Gautham JanardhanCommented:
try passing


and then merging the two datasets' after the opertion..
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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jackjohnson44Author Commented:
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.
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.

jackjohnson44Author Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

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.