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

Comment Utility
Windows or web?
LVL 18

Expert Comment

by:Ajay Sharma
Comment Utility
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

Comment Utility
LVL 29

Expert Comment

by:Gautham Janardhan
Comment Utility
try passing


and then merging the two datasets' after the opertion..
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

LVL 34

Accepted Solution

Sancler earned 500 total points
Comment Utility
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

Comment Utility
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

Comment Utility
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

Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now