[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

acceptchanges causing autoscroll

Hi,
I have a datagridview and I want to calculate the total for one of the columns when a cell value is changed. The only way I've found to do this so far is to call acceptchanges for the datatable. However, acceptchanges makes the datagridview scroll away from the value that was just entered.

The total will calculate without calling acceptchanges, but it does not include the currently changed cell value. I haven't found an event to use where the cell value updates without calling acceptchanges.

What can I try here?

Thx,
Dave
0
coperations07
Asked:
coperations07
  • 7
  • 4
  • 3
3 Solutions
 
CodeCruiserCommented:
You can try using the Cell_Validating event but the trouble is that the new value is only committed to the datatable when the cell loses focus.

http://windowsclient.net/learn/video.aspx?v=13416
0
 
coperations07Author Commented:
Yeah I tried the validating and validated events and they didn't get it done either. ..
0
 
grayeCommented:
Let's be clear about something.... Many people completely misunderstand what AcceptChanges does.  It may not be doing what you expect it do.  This is because th name is somewhat misleading...  instead of "saving the current changes", it actually does the exact opposite!  AcceptChanges will *remove* all of the internal markers where changes have been made.  This makes it "look like" there are no changes that need to be saved (even though changes were actually made).

The DataAdapter uses this field to determine what should be done when updating the DataTable back to the underlying database.  So, AcceptChanges will "erase all knowledge of any changes", and therfore the DataAdapter will not send any changes to the backend database (since it didn't detect any).

... sorry for the interruption, we now return you to your original question....
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
coperations07Author Commented:
Thanks graye.  AcceptChanges isn't necessarily something that I want to use.  It's just the only thing I've come across that gets close to the results I am looking for.  I can use the update method of the datagridview to display the value before the code finishes executing, but that doesn't help me when it comes to a calculation with the datatable.
0
 
grayeCommented:
One of the easiest ways to accomplish what you're after is to use Language Intergrated Query (LINQ).  This will allow you to do an SQL-like query on the locally-cached DataTable.

It would look something like this:

 Dim dt = DataTable.AsEnumerable()
 Dim numSum = (From n In dt select n.Field(Of Integer)("number")).Sum()

http://en.wikipedia.org/wiki/Language_Integrated_Query

0
 
coperations07Author Commented:
LINQ seems interesting, but I haven't been able to get it to work for me yet?  I am using VS2005. AsEnumerable() isn't recognized by intellisense after datatable.
do I have to import something?
This is where I'm at:

            Dim myLINQ = (from row in tSorter.AsEnumerable() _
                            Where Selected_CF > 0
                            Select row.Min_Cht).sum()

thanks,
Dave
0
 
grayeCommented:
Yeah, LINQ requires .Net 3.5 (VS2008).

Sorry for the wild goose chase
0
 
coperations07Author Commented:
no prob.  I'm about to upgrade to 2008 or 2010 shortly so I'll take a look then.

I think I may have found a way to do it. Still working on getting it just right..

        If Not IsDBNull(tSorter.Compute("Sum(Min_Cht)", "Selected_CF > 0")) Then
            dMinCht = tSorter.Compute("Sum(Min_Cht)", "Selected_CF > 0")
            dMinCht = dMinCht + Me.dgResults.CurrentRow.Cells("Min_Cht").Value
0
 
CodeCruiserCommented:
You are doing the compute twice above

Dim objCompute As Object = tSorter.Compute("Sum(Min_Cht)", "Selected_CF > 0")

If Not IsDBNull(objCompute) And Not IsNothing(objCompute) Then
            dMinCht = objCompute
            dMinCht = dMinCht + Me.dgResults.CurrentRow.Cells("Min_Cht").Value
End If
0
 
coperations07Author Commented:
Thanks CodeCruiser,

The prob I'm running into now happens when a row is edited after the initial entry.  When this happens the Compute will include the edited row, but then I am adding it again.  How can I handle this?
0
 
CodeCruiserCommented:
> dMinCht = dMinCht + Me.dgResults.CurrentRow.Cells("Min_Cht").Value
Why not remove the above line then? Is it required when adding a new line? Or do you mean that the old value is included in the compute and you want to use the new value?
0
 
coperations07Author Commented:
When an entry is made to the dgv it doesn't get included in the Sum when I use the Compute function, so I get the value of the current cell from the dgv and add it in.  If it is edited again then the Sum will pick up the previous entry.  I'm using the cellchanged event and the datatable doesn't seems to update until after the event finishes.
0
 
CodeCruiserCommented:
Did you try using the CellEndEdit event?
0
 
coperations07Author Commented:
Thanks for the help guys.  The CellEndEdit didn't work for me either, but I used an IF..Else to get what I needed.  Thanks for the LIINQ suggestion Graye, I will check it out when I upgrade.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now