Force update datagrid

G

Guest

I have a Parent and child datagrid pair. Before I can add a new child to a
new parent I have to change the focus in the parent table to another row
before the parent is added and thus avaibale for adding children to it.

How can I force this same update to the dataset when my focus moves from the
parent datagrid to the child datagrid?
 
W

W.G. Ryan - MVP

Assuming this is a Winforms grid (and that I understood the problem
correctly), you can call EndEdit on the row in the gridwhich will end the
edit and add the row to the table. At that point, you can add a child to
that row.

Here's an example. I have a grid and I add a new row but have not left the
given row - on Enter of a textbox (which could be any control), I fire this
code:
MessageBox.Show(dt.Rows.Count.ToString()); //4

dt.Rows[dt.Rows.Count-1].EndEdit();

MessageBox.Show(dt.Rows.Count.ToString()); //5



So you just need to find the row in question and call EndEdit on it.
Depending on sorting and what have you, you may or may not be able to use
Count-1 which is what I used here but this should give you an idea how it
works).

Let me know if not.



HTH,

Bill
 
G

Guest

I can't seem to get this one right and it is very anoying.
The only event it seems I can use is the 'lost focus' event of the
parentgrid or the 'gotfocus' event of the child grid since the user goes to
the new empty record of the child grid directly after typing values in the
new record in the parent grid.

Since my datagrid is bount to a dataset table, I tried:

DataSet.Tables("ParentTable").Rows(ChildDataGrid.CurrentRowIndex).EndEdit()

but the parent column in the child grid remains unpopulated, unless I first
change the focus in the parentgrid from and back to the new record in the
parent grid.

This is however a nuisance since a user would add a new parent record and
directly shift the focus to the child table to add records there...

W.G. Ryan - MVP said:
Assuming this is a Winforms grid (and that I understood the problem
correctly), you can call EndEdit on the row in the gridwhich will end the
edit and add the row to the table. At that point, you can add a child to
that row.

Here's an example. I have a grid and I add a new row but have not left the
given row - on Enter of a textbox (which could be any control), I fire this
code:
MessageBox.Show(dt.Rows.Count.ToString()); //4

dt.Rows[dt.Rows.Count-1].EndEdit();

MessageBox.Show(dt.Rows.Count.ToString()); //5



So you just need to find the row in question and call EndEdit on it.
Depending on sorting and what have you, you may or may not be able to use
Count-1 which is what I used here but this should give you an idea how it
works).

Let me know if not.



HTH,

Bill

PaulNaude said:
I have a Parent and child datagrid pair. Before I can add a new child to a
new parent I have to change the focus in the parent table to another row
before the parent is added and thus avaibale for adding children to it.

How can I force this same update to the dataset when my focus moves from
the
parent datagrid to the child datagrid?
 
W

W.G. Ryan - MVP

Trap the Leave event, that should work as soon as you click out of the grid
on any other control. Let me know if that still doesn't do it.
PaulNaude said:
I can't seem to get this one right and it is very anoying.
The only event it seems I can use is the 'lost focus' event of the
parentgrid or the 'gotfocus' event of the child grid since the user goes
to
the new empty record of the child grid directly after typing values in the
new record in the parent grid.

Since my datagrid is bount to a dataset table, I tried:

DataSet.Tables("ParentTable").Rows(ChildDataGrid.CurrentRowIndex).EndEdit()

but the parent column in the child grid remains unpopulated, unless I
first
change the focus in the parentgrid from and back to the new record in the
parent grid.

This is however a nuisance since a user would add a new parent record and
directly shift the focus to the child table to add records there...

W.G. Ryan - MVP said:
Assuming this is a Winforms grid (and that I understood the problem
correctly), you can call EndEdit on the row in the gridwhich will end the
edit and add the row to the table. At that point, you can add a child to
that row.

Here's an example. I have a grid and I add a new row but have not left
the
given row - on Enter of a textbox (which could be any control), I fire
this
code:
MessageBox.Show(dt.Rows.Count.ToString()); //4

dt.Rows[dt.Rows.Count-1].EndEdit();

MessageBox.Show(dt.Rows.Count.ToString()); //5



So you just need to find the row in question and call EndEdit on it.
Depending on sorting and what have you, you may or may not be able to use
Count-1 which is what I used here but this should give you an idea how it
works).

Let me know if not.



HTH,

Bill

PaulNaude said:
I have a Parent and child datagrid pair. Before I can add a new child to
a
new parent I have to change the focus in the parent table to another
row
before the parent is added and thus avaibale for adding children to it.

How can I force this same update to the dataset when my focus moves
from
the
parent datagrid to the child datagrid?
 
G

Guest

Nope! No difference :-(

W.G. Ryan - MVP said:
Trap the Leave event, that should work as soon as you click out of the grid
on any other control. Let me know if that still doesn't do it.
PaulNaude said:
I can't seem to get this one right and it is very anoying.
The only event it seems I can use is the 'lost focus' event of the
parentgrid or the 'gotfocus' event of the child grid since the user goes
to
the new empty record of the child grid directly after typing values in the
new record in the parent grid.

Since my datagrid is bount to a dataset table, I tried:

DataSet.Tables("ParentTable").Rows(ChildDataGrid.CurrentRowIndex).EndEdit()

but the parent column in the child grid remains unpopulated, unless I
first
change the focus in the parentgrid from and back to the new record in the
parent grid.

This is however a nuisance since a user would add a new parent record and
directly shift the focus to the child table to add records there...

W.G. Ryan - MVP said:
Assuming this is a Winforms grid (and that I understood the problem
correctly), you can call EndEdit on the row in the gridwhich will end the
edit and add the row to the table. At that point, you can add a child to
that row.

Here's an example. I have a grid and I add a new row but have not left
the
given row - on Enter of a textbox (which could be any control), I fire
this
code:
MessageBox.Show(dt.Rows.Count.ToString()); //4

dt.Rows[dt.Rows.Count-1].EndEdit();

MessageBox.Show(dt.Rows.Count.ToString()); //5



So you just need to find the row in question and call EndEdit on it.
Depending on sorting and what have you, you may or may not be able to use
Count-1 which is what I used here but this should give you an idea how it
works).

Let me know if not.



HTH,

Bill

I have a Parent and child datagrid pair. Before I can add a new child to
a
new parent I have to change the focus in the parent table to another
row
before the parent is added and thus avaibale for adding children to it.

How can I force this same update to the dataset when my focus moves
from
the
parent datagrid to the child datagrid?
 
C

Cor Ligthert [MVP]

PaulNaude,

Not nice to do, however if everything fails with a datagrid than can the
Paint event is possible be your last change to use.

The datagrid has some things which seems to be not really complete, which
are renewed with the datagridview in the next version.

Here a link to our VB-Tips website where that is used as well in a situation
I could not make a simple sample to show things.

http://www.vb-tips.com/default.aspx?ID=30d9d2fd-9f10-4928-b7c8-1def3152436f

I hope this helps,

Cor
 
G

Guest

Thanks but this seems to create more problems (since the paint event is
triggered before the datagrid is populated etc.)

I am considering to code a 'change row' event which simulates the user
changing the row in the parent grid before going to the child grid. This
unfortunaterly brings along a number of other problems, like, where do you go
if the parent table was empty to start off with? and, you have to prevent
this event from firing itself, and this should only occur when a new record
was entered etc. etc.

Any other ideas?
 
C

Cor Ligthert [MVP]

Paul,

Did you understand that I did mean the other datagrid, not the one you want
to update.

Cor
 
G

Guest

I did not, but tried this now and no success.

It seems that when the user changes row focus in the parent grid, some or
other event is triggered that updates the dataset. If I can identify this
event, I could probable call it in the 'leave' event.
 
C

Cor Ligthert [MVP]

Paul,
It seems that when the user changes row focus in the parent grid, some or
other event is triggered that updates the dataset. If I can identify this
event, I could probable call it in the 'leave' event.
This above is correct, it is as well done by a
currencymanager.endcurrentedit.

However in my idea you don't give us much information what you try to
achieve.

I gues that you when a row is added in a child grid, automaticly a row is
added to the parent grid.

If this by example is wanted, than I would probably make my parent grid read
only and even try to protect that it would got focus. In most other
solutions for this you would probably become in my idea in almost impossible
to track logic what a user could do.

Just my thought,

Cor
 
G

Guest

No, the child record is created after the parent record.

Background:
My child datagrid is in sync with the parent grid as follows:
childdatagrid.DataSource = DataSet
childdatagrid.DataMember = "ParentTable.ParentChildRelationship"

When ever a parent record is selected in the parent grid, its children is
displayed in the child grid.

My problem is basically only a sequence of events from the user's side that
I have to cater for when adding new records.

My problem is that, when the user enters a new parent record, and change
focus DIRECTLY to the child datagrid to enter a new record there, the parent
column in the child table is still empty. (my child record has two columns -
the first indicates who the parent is, the second is the child name). Since
my child grid is 'filtered' according to the parent, all the values in the
parent column displayed are identical and thus I hide it from the user.

Now, since the dataset has not yet updated, the dataset can't add this new
child record because the parent value is still empty and the database don't
allow empty records in the parent field. Since I hide this column, the user
can't enter it either and shouldn't, because the software should do it for
him.

If I enter a new parent record in the parent grid, but first change focus
within the parentgrid by selecting any other row, and then put the focus back
in this newly added record, I may then proceed to the child datagrid and add
new records there, since the dataset has now updated to include the new
parent, and my parent column in the child grid is automatically populated
with the correct parent value without the user even being aware of what is
happening behind the scences.

All I want, is that the same event which updates the dataset when I change
the focus in the parent datagrid by changing focus from a new record to an
existing record, should also fire when I don't change the focus inside the
parentgrid, but change the focus directly to the child grid.

I hope I explains this well!
 
G

Guest

Cor

Thanks - I've used that code you've written for me to great use but if I use
the 'fake' relationship in this case, I will need to add the parent column
values in any case, so I might just as well do it now (and prevent the empty
field in the first place).

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

Top