passing datasets between forms

Posted on 2007-03-23
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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

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 500 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Get distinct values from excel or comm seperated file 4 30 cycle through results of a sql query and capture variables 3 18
VS 2017 18 36
SSIS Start 2 20
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

839 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