copying an object by value

  • Thread starter Thread starter Hemang Shah
  • Start date Start date
H

Hemang Shah

I think what i'm trying to do is passing by value.

Here is the scenario:

Main Form - which calls a Search form.
- I pass Mainform as a reference so that I can pass data between the two.
Code:

public frmSearch(Form1 f)

{

InitializeComponent();

frmMain = new Form1();

frmMain = f;

dvSearch = new System.Data.DataView();

dvSearch = frmMain.dvClient;

this.BindingContext = frmMain.BindingContext;

dgvSearchDisplay.DataSource = dvSearch;

}

I have a dataView on the main form called (dvClient)

I have a datagrid on the SearchForm which I want to filter / sort without
affecting the main form.

So I create a copy of the dataview from main form into a dataview called
dvSearch.

But now when I apply a row filter to this dvSearch, the main form is getting
affected to.

The reason why making a copy of the dataview is important because I want to
retain the currencymanager value.

So that once a record is selected in the datagrid in the searchform, the
fields are updated on the main form.

Hope this explains what I'm trying to achieve.

Thanks
 
Hemang Shah said:
[...]
frmMain = new Form1();
frmMain = f;

Notice that the first one of these two lines is superfluous, since the
frmMain that you initialized with the "new" gets immediately overwritten wit
"f".
dvSearch = new System.Data.DataView();
dvSearch = frmMain.dvClient;

Same thing here. The first "new" is useless, since the second line
overwrites dvSearch with a new value.
[...]
So I create a copy of the dataview from main form into a dataview called
dvSearch.

No, the previous line does not copy the dataview. It only copies the
reference to the dataview. Both frmMain.dvClient and dvSearch still point to
the same dataview.
But now when I apply a row filter to this dvSearch, the main form is
getting affected to.

Yes, because dvSearch points to the same dataview that frmMain has, so
changing one of its properties affects frmMain.
The reason why making a copy of the dataview is important because I want
to retain the currencymanager value.

So that once a record is selected in the datagrid in the searchform, the
fields are updated on the main form.

Hope this explains what I'm trying to achieve.

You will need to rethink it a little bit. You can create a second
dataview from the same datatable from which you created the first one:
dvSearch = new System.Data.DataView(frmMain.dvClient.Table);
But this does not relate the second dataview to the currencymanager, so
you will have to elaborate your code a little more.
 
Thanks for the reply Alberto

I got that far. Yours is a bit better than my idea, but what I did is:

On the main form, I created another dataview pointing to the same table in
the same dataset and I was sending that to the child form.

But the problem is, Now when a user clicks on an item in the child form, the
main form doesn't point to that record (because the currencymanagers are
different).

I've been searching high & low on how to sync two dataview bound to the same
datatable. I can't find anyways of doing it.

I can't even think how to do it manually if currencymanager is not working
out.

any advice?

Thanks

Alberto Poblacion said:
Hemang Shah said:
[...]
frmMain = new Form1();
frmMain = f;

Notice that the first one of these two lines is superfluous, since the
frmMain that you initialized with the "new" gets immediately overwritten
wit "f".
dvSearch = new System.Data.DataView();
dvSearch = frmMain.dvClient;

Same thing here. The first "new" is useless, since the second line
overwrites dvSearch with a new value.
[...]
So I create a copy of the dataview from main form into a dataview called
dvSearch.

No, the previous line does not copy the dataview. It only copies the
reference to the dataview. Both frmMain.dvClient and dvSearch still point
to the same dataview.
But now when I apply a row filter to this dvSearch, the main form is
getting affected to.

Yes, because dvSearch points to the same dataview that frmMain has, so
changing one of its properties affects frmMain.
The reason why making a copy of the dataview is important because I want
to retain the currencymanager value.

So that once a record is selected in the datagrid in the searchform, the
fields are updated on the main form.

Hope this explains what I'm trying to achieve.

You will need to rethink it a little bit. You can create a second
dataview from the same datatable from which you created the first one:
dvSearch = new System.Data.DataView(frmMain.dvClient.Table);
But this does not relate the second dataview to the currencymanager,
so you will have to elaborate your code a little more.
 
Thanks for the reply Alberto

I got that far. Yours is a bit better than my idea, but what I did is:

On the main form, I created another dataview pointing to the same table in
the same dataset and I was sending that to the child form.

But the problem is, Now when a user clicks on an item in the child form, the
main form doesn't point to that record (because the currencymanagers are
different).

First, a caveat: I've never used datagrids or currencymanagers.
However, I have done the sort of thing you're trying to do in other
situations.

There are two possibilities (and you haven't really defined which one
you're trying to achieve): 1) you want the main form to update as soon
as the user clicks an item on the search form; 2) you want the main
form to update as soon as the user clicks and item on the search form
and dismisses it.

In other words, is the search form running in parallel with the main
form (searchForm.Show()) or is it a dialog (searchForm.ShowDialog())?

In the first case, the solution is this: define an event on your
search form:

public event System.EventHandler UserSelectedItem;

and a property:

public XXX SelectedItem { get { ... } }

Have your main form subscribe to the event. Whenever the event is
raised, the main form will read the SelectedItem property, find the
same item in its grid, and select it.

On the other hand, if your search form is a dialog, you don't need the
event, just the property. When the dialog returns, if the user clicked
OK, the main form just reads the SelectedItem property and sets its
current item accordingly.
 
Thanks Bruce

Either option will work for me. Eventually when the user exists the
Searchform, I want the required record to show on the main form, if it shows
even before the searchform is closed, it doesn't matter. All that matters
is once the user selects a record in the datagrid on the search form, and
clicks ok (to close it), the main form displays that record.

My Search Form is a .showDialog(), as I don't want them to do data entry
when the search form is still open.

Secondly, the main form doesn't use datagrid, it is the search form which
uses the datagrid to select the record.

The main form has multiple text boxes, combo boxes, lists...( a typical data
entry form).

All the controls are bound to a dataview.

I don't know how to select a particular record in a dataview.

Because you cannot use find on a dataview unless the column in the dataview
is sorted. Which is not the case in mine.
 
Hemang,

You are stating so many things which are untrue, that it is almost
impossible to answer.

However to answer one, there are 4 methods to find a row in a dataview. The
easiest one is to use the rowfilter.

Cor
 
Cor,

Please correct me what i'm wrong about, so that I can learn.

I'm curious, you mention finding by using the rowfilter..

isn't searching & filtering different ?

I'm using RowFilter to filter the datagrid, here is the code:

dvSearch.RowFilter = "GivenName LIKE '"
+txtSearchFirstName.Text.Replace("'","''") + "%'";

dgvSearchDisplay.Refresh();

But what I want is the record selected in this datagrid, to be displayed on
the main form, which is bound by a different dataview (but point to the same
data table).



My question is, how can we sync two different currencymanagers?

I'm no guru, just getting my hands dirty with some ado.net code (which is
obvious in my basic questions)

Thanks
 
See my other answer

Hemang Shah said:
Cor,

Please correct me what i'm wrong about, so that I can learn.

I'm curious, you mention finding by using the rowfilter..

isn't searching & filtering different ?

I'm using RowFilter to filter the datagrid, here is the code:

dvSearch.RowFilter = "GivenName LIKE '"
+txtSearchFirstName.Text.Replace("'","''") + "%'";

dgvSearchDisplay.Refresh();

But what I want is the record selected in this datagrid, to be displayed
on the main form, which is bound by a different dataview (but point to the
same data table).



My question is, how can we sync two different currencymanagers?

I'm no guru, just getting my hands dirty with some ado.net code (which is
obvious in my basic questions)

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top