updating datagrid after returning

G

Guest

After clicking btnDeleteSize_Click in frmSize, I move to frmSizeDelete, allow
deleting a Size using a datagrid, and then return to frmSize and want to
update the datagrid in frmSize to reflect the deletion. I can't figure out
how to get the datagrid in frmSize updated, although the datagrid in
frmSizeDelete shows the deletion properly.

In frmSizeDelete the size is properly deleted when the user clicks the row
in the datagrid and presses the delete key:

private void frmSizeDelete_Load(object sender, System.EventArgs e)
{
strCommandText = "SELECT SizeID, SizeName FROM tblSize WHERE SizeID = '" +
strCurrentSize + "'";
this.oleDbSelectCommand1.CommandText = strCommandText;
oleDbDataAdapter1.Fill(dsSizeDelete1);
}

private void frmSizeDelete_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
oleDbDataAdapter1.Update(dsSizeDelete1);
}

However, when I return to frmSize I can't figure out how to show the updated
datagrid.
The following code has no errors, but doesn't work:

private void btnDeleteSize_Click(object sender, System.EventArgs e)
{
// dgSize.ReadOnly = false; (dgSize is readonly, does this matter in showing
the update?)
DialogResult button =
MessageBox.Show("Are you sure you want to delete this size? This size will
be deleted from ALL product parts!!!", "Delete Size",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
if (button ==DialogResult.Yes)
{
Form newSizeDeleteForm = new frmSizeDelete(txtSizeID.Text);
newSizeDeleteForm.Show();
dsSize1.Clear();
oleDbDataAdapterSize.Fill(dsSize1);
oleDbDataAdapterSize.Update(dsSize1);
dgSize.Update();
dgSize.Show();

I added these last 5 statements in the hopes of making the datagrid in
frmSize show the change, and I don't know if any of them are needed, but they
do not show the change as expected. What should I do?

Thanks,
Pam
 
K

Kevin Yu [MSFT]

Hi Pam,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to delete a row of the
DataSet, update it to data source and update the data grid. If there is any
misunderstanding, please feel free to let me know.

Based on the code you have provided, I didn't see any delete code to delete
that row in the DataSet table. When deleting and refreshing, we have 2
choices.

The first is to delete in the current DataSet in frmSize and use an
DataAdapter to update it to the data source. Since the datagrid in bound to
DataSet, when data in it changes, it will automatically show the changes.
For example, I need to delete a row dr.

dr.Delete();
oleDbDataAdapterSize.Update(dsSize1);

The second is to delete data directly in the database and then re-fill the
DataSet with updated data.

dsSize1.Clear();
oleDbDataAdapterSize.Fill(dsSize1);

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Thank you, but this didn't help since it is already what I am doing. It
still doesn't work.

To answer your question, I used the datagrid built in features to delete the
row - user highlights the row and presses delete. That part works fine. It
also shows the deletion fine in the dgSizeDelete in the frmSizeDelete. It is
when I return to the calling form, frmSizes and want the Datagrid, dgSize, in
the calling form to update that I have a problem.

As you can see from my initial post,
dsSize1.Clear();
oleDbDataAdapterSize.Fill(dsSize1);
is exactly what I did. And dgSize did not show the change in frmSize when I
return to it. This is why I added the 3 lines of code after it to try to get
the datagrid to show the change.

I just tried it again, commenting out the 3 additional lines of code. It
still doesn't work.

What I think you don't understand is that your example of code is not the
datagrid that I am having a problem with, it is the datagrid I return to in
the calling form that is a problem.

Pam
 
K

Kevin Yu [MSFT]

Hi Pam,

The dataGrid which show dsSize1 doesn't get updated, because when you call
newSizeDeleteForm.Show();, you assume that the code behind this line will
be suspended. They will wait for the newSizeDeleteForm to close, update the
database and continue executing with updated values.

Actually, the code didn't suspend. It goes down before newSizeDeleteForm
got a chance to delete data on the database, which mean they we run in two
threads. I suggest you to use newSizeDeleteForm.ShowDialog() instead of
newSizeDeleteForm.Show(). That might help you out of the problem.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Sorry, it still doesn't work. What you said makes sense, so I don't
understand why it is not working. Any other suggestions?

Or is there a better way to write my code? What I am trying to do is to
drill down, so I go from frmPart to frmSize and then I either Add, Edit or
Delete a Size. Then I come back up to work on the next part.

Or perhaps you want me to zip my code to you?

I really need to figure out a way to get this project finished so I can get
paid and move on to other things. I'm losing money with all the time I've
spent trying to find ways around things I can't do. Any suggestions would be
appreciated.

Thanks,
Pam
 
G

Guest

Actually, it almost works. I go from frmSize to frmSizeDelete, delete the
Size and return to frmSize and it still doesn't show that it works. But then
I go back to frmSizeDelete (which now shows nulls because the Size actually
is deleted), don't do anything but close the frmSizeDelete a second time, and
then when I go back to frmSize the original deletion shows up. Why doesn't
it work the first time? How can I make it work the first time?

Thanks,
Pam
 
K

Kevin Yu [MSFT]

Hi Pam,

This is strange. Could you please zip the code and send it to my email with
the repro steps? Removing 'online' from the no spam alias is my real email.
It will be faster for us to find out the problem with the repro code.
Thanks for your cooperation.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
K

Kevin Yu [MSFT]

Hi Pam,

When I changed newSizeDeleteForm.Show(); to
newSizeDeleteForm.ShowDialog();, everything works fine. The DataGrid gets
updated when Done is clicked. HTH.

I have send the changed code to your mailbox. Thanks.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Kevin,

I apologize, I see that in my code I incorrectly put the ShowDialog(); on
the wrong button. Thank you for your correction.

I still am wondering... it deletes fine in YOUR code that you sent me. But
in my code it appears to show as fully deleted once I click back into the
datagrid, while yours doesn't need that. I'm not complaining that much since
at least it now works. I was just wondering if there is anything else that
you remember as changing in your code that isn't in mine?

Is there any way to search two copies of code in two separate Solutions and
find only the differences between the two copies?

Is there another way to ask VS.NET to check my code more completely other
than pressing the Start-Debug button? My code seems flaky and I'm wondering
if it is damaged in some way that I'm not aware of.

But regardless of my flaky result, yours definitely works. THANKS!!!!

Pam
 
K

Kevin Yu [MSFT]

Hi Pam,

As far as I can remember, I only changed some code in that button event
handler. Nothing else.

There are some tools for us to compare codes such are WinDiff.exe. It is
shipped with platform SDK. However, there are some 3rd-party front-end for
us to use it easier. You can find one from the following link:

http://www.codeproject.com/tools/runwindiff.asp

There doesn't seem to be any way in VS.NET to check our code more
completely. Since we can only get compilation error at compile time and
runtime error at runtime.

Thanks!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

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