Home All Groups Group Topic Archive Search About

copying an object by value

Author
7 Apr 2007 10:06 PM
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
Author
8 Apr 2007 7:42 AM
Alberto Poblacion
"Hemang Shah" <hem***@hemang.net> wrote in message
news:ehyt6DWeHHA.4172@TK2MSFTNGP05.phx.gbl...
> [...]
> 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.
Are all your drivers up to date? click for free checkup

Author
8 Apr 2007 4:49 PM
Hemang Shah
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

Show quoteHide quote
"Alberto Poblacion" <earthling-quitaestoparacontes***@poblacion.org> wrote
in message news:eHcKzFbeHHA.4868@TK2MSFTNGP06.phx.gbl...
> "Hemang Shah" <hem***@hemang.net> wrote in message
> news:ehyt6DWeHHA.4172@TK2MSFTNGP05.phx.gbl...
>> [...]
>> 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.
>
>
Author
8 Apr 2007 6:02 PM
Bruce Wood
On Apr 8, 9:49 am, "Hemang Shah" <hem***@hemang.net> wrote:
> 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.
Author
8 Apr 2007 8:32 PM
Hemang Shah
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.


Show quoteHide quote
"Bruce Wood" <brucew***@canada.com> wrote in message
news:1176055379.707713.3380@o5g2000hsb.googlegroups.com...
> On Apr 8, 9:49 am, "Hemang Shah" <hem***@hemang.net> wrote:
>> 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.
>
Author
8 Apr 2007 11:45 PM
Cor Ligthert [MVP]
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

Show quoteHide quote
"Hemang Shah" <hem***@hemang.net> schreef in bericht
news:%23betvzheHHA.3960@TK2MSFTNGP02.phx.gbl...
> 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.
>
>
> "Bruce Wood" <brucew***@canada.com> wrote in message
> news:1176055379.707713.3380@o5g2000hsb.googlegroups.com...
>> On Apr 8, 9:49 am, "Hemang Shah" <hem***@hemang.net> wrote:
>>> 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.
>>
>
>
Author
9 Apr 2007 12:06 AM
Hemang Shah
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



Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:Org9HejeHHA.4564@TK2MSFTNGP03.phx.gbl...
> 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
>
> "Hemang Shah" <hem***@hemang.net> schreef in bericht
> news:%23betvzheHHA.3960@TK2MSFTNGP02.phx.gbl...
>> 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.
>>
>>
>> "Bruce Wood" <brucew***@canada.com> wrote in message
>> news:1176055379.707713.3380@o5g2000hsb.googlegroups.com...
>>> On Apr 8, 9:49 am, "Hemang Shah" <hem***@hemang.net> wrote:
>>>> 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.
>>>
>>
>>
>
>
Author
9 Apr 2007 6:08 AM
Cor Ligthert [MVP]
See my other answer

Show quoteHide quote
"Hemang Shah" <hem***@hemang.net> schreef in bericht
news:e0AIgrjeHHA.5044@TK2MSFTNGP06.phx.gbl...
> 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
>
>
>
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
> news:Org9HejeHHA.4564@TK2MSFTNGP03.phx.gbl...
>> 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
>>
>> "Hemang Shah" <hem***@hemang.net> schreef in bericht
>> news:%23betvzheHHA.3960@TK2MSFTNGP02.phx.gbl...
>>> 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.
>>>
>>>
>>> "Bruce Wood" <brucew***@canada.com> wrote in message
>>> news:1176055379.707713.3380@o5g2000hsb.googlegroups.com...
>>>> On Apr 8, 9:49 am, "Hemang Shah" <hem***@hemang.net> wrote:
>>>>> 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.
>>>>
>>>
>>>
>>
>>
>
>

Bookmark and Share