[RESOLVED] DataGridView update is ok, second update fails.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: [RESOLVED] DataGridView update is ok, second update fails.

  1. #1
    Join Date
    Feb 2011
    Posts
    7

    [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!

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: DataGridView update is ok, second update fails.

    When are you calling that code? You can download the samples from here
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Feb 2011
    Posts
    7

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: DataGridView update is ok, second update fails.

    That's why. Update the grid with a different button, when they're done!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Feb 2011
    Posts
    7

    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!

  6. #6
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: DataGridView update is ok, second update fails.

    Most grids have an EDIT mode that lets the user change field(s)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Feb 2011
    Posts
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center