-
February 13th, 2011, 02:17 AM
#1
[RESOLVED] DataGridView update is ok, second update fails.
Hi!
If you see some ugly coding, do let me know. I am beginning to work with .NET technology after several years of VB6 and C++ and I want to learn as fast as possible.
My problem is with a DGV. It shows the prices of several store items, and I intend to let the user modify those prices by entering a % in a textbox (radio button states if that % is positive or negative) or by editing cell by cell manually.
As editing prices is kind of sensitive, what I do is loop through the DGV, add or substract the % engtered in the corresponding column and ask for a confirmation after whole DGV loop is done. If user agrees, DB is updated with new values. No problem here.
If user denies (in case he/she made a mistake), I restore grid's DataSource to a copy I made before calculating new percentages and this automatically refreshes my grid to its original state.
The issue is that if a user denies the operation for a 2nd time, this 2nd execution of the code fails at showing the original values, disabling the pseudo-rollback function.
Here is the code:
.
.
.
If MessageBox.Show(mensaje, "Actualizo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.Yes Then
If rdbAumento.Checked Then
variacion = Convert.ToInt32(txPorcentaje.Text)
For i = 0 To gridPrecios.Rows.Count - 2
gridPrecios.Item(5, i).Value = (gridPrecios.Item(5, i).Value * (100 + variacion) / 100)
Next
Else
variacion = Convert.ToInt32(txPorcentaje.Text)
For i = 0 To gridPrecios.Rows.Count - 2
gridPrecios.Item(5, i).Value = (gridPrecios.Item(5, i).Value * (100 - variacion) / 100)
Next
End If
End If
If MsgBox("Desea guardar los cambios realizados?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "WARNING") = MsgBoxResult.Yes Then
da.Update(ds, "datos")
Else
'This is the line that fails upon 2nd execution:
gridPrecios.DataSource = dsrestore.Tables(0)
End If
Else
.
.
.
Please, remember this works OK if user denis once. What am I missing regarding DataSources or its Tables? Is there anything like .Redraw?
Btw, I tried update, refresh and I also used a sleep function (this helped me a few times back in VB6...).
Thanks in advance!
-
February 13th, 2011, 01:09 PM
#2
Re: DataGridView update is ok, second update fails.
When are you calling that code? You can download the samples from here
-
February 13th, 2011, 01:38 PM
#3
Re: DataGridView update is ok, second update fails.
Thanks for your reply!
That code is executed in a _Click event.
The user clicks the radio button to select increase or decrease of prices, enters a number in a textbox and clicks a button that triggers this code. Grid is updated with new values and shown automatically.
-
February 13th, 2011, 01:53 PM
#4
Re: DataGridView update is ok, second update fails.
That's why. Update the grid with a different button, when they're done!
-
February 13th, 2011, 04:33 PM
#5
Re: DataGridView update is ok, second update fails.
I'll write a short routine for that.
Could you elaborate or give me a link that explains why I need to do so?
Tbh, if I were the user, I wouldn't want to click a button to refresh a grid. Is there no other way to automatically update the grid after it was looped through?
Btw, I checked your first link and is really resourceful, and even though I couldn't use those examples to understand my problem, I'll keep it at hand!
Thanks again!
-
February 13th, 2011, 05:19 PM
#6
Re: DataGridView update is ok, second update fails.
Most grids have an EDIT mode that lets the user change field(s)
-
February 14th, 2011, 10:51 AM
#7
Re: DataGridView update is ok, second update fails.
I just saw BeginEdit and EndEdit functions, I'll take a look at those.
Also, I kind of found a solution (which is not what I wanted, but is working though). Now, I reset the the datasource of that grid and refill it through the adapter.
I'll test it out with a grid with 10.000 records to check it's performance.
Thanks for your feedback mate!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|