Results 1 to 7 of 7

Thread: Problem updating value in a DataGrid cell. [Resolved]

  1. #1

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Question Problem updating value in a DataGrid cell. [Resolved]

    I have a DataGrid, which has 3 columns:
    - Net Price
    - Quantity
    - Total Price


    I want the DataGrid to act like this:
    - Net Price is set.
    - User enter Quantity.
    - Total Price is calculated automatically (Net Price * Quantity).
    - Total Price column will be updated automatically.


    So I wrote the code like this:
    Code:
    'Calculate total price
        Private Sub DataGrid_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid.CurrentCellChanged
            Dim intPreviousRow As Integer
            Dim intColumn As Integer = DataGrid.CurrentCell.ColumnNumber
    
            If DataGrid.CurrentCell.RowNumber <> 0 Then
                intPreviousRow = DataGrid.CurrentCell.RowNumber - 1
            Else
                intPreviousRow = DataGrid.CurrentCell.RowNumber
            End If
    
            Dim intTaken As Integer = DataGrid.Item(intPreviousRow, intColumn)
            Dim dblNetPrice As Double = DataGrid.Item(intPreviousRow, intColumn + 1)
            Dim dblTotalPrice As Double = intTaken * dblNetPrice
            DataGrid.Item(intPreviousRow, intColumn + 2) = dblTotalPrice
        End Sub
    Everytime I enter the Quantity, this error message pops up:
    "An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll

    Additional information: The ListManager's position must be equal to rowNum."

    The error occurs at the last line:
    Code:
    DataGrid.Item(intPreviousRow, intColumn + 2) = dblTotalPrice

    Why? I can't solve this. I think the code I wrote cannot work.
    Can anyone help me to solve, or write a new code for this situation?


    Another question, after I have edited the DataGrid, if I want to save it to the database, I need to execute the DataAdapter's Update Command, right?
    So how to code for this? Can anyone show me?

    Please guide. Thank you.
    Last edited by albertlse; Aug 21st, 2003 at 08:27 PM.

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    You dotn have to write thant bunch of code for this, just:
    Imagine you have columns Net,Quantitiy,and total in mytable. you can use:
    mytable.Columns("Total").Expression="Net * Quantity"

    I attached a sample for you. put the database in C:\ to work.
    Attached Files Attached Files
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Question

    I follow exactly what you did, and I added this code:

    Code:
    DistributeDataSet.Tables(0).Columns(7).Expression = "Taken * NetPrice"
    But I get this error:

    An unhandled exception of type 'System.Data.EvaluateException' occurred in system.data.dll
    Additional information: Cannot perform '*' operation on System.String and System.String.
    why? is there any solution?

    Please guide once again. Thank you.

  4. #4
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Its seems that you have defined those fields "Taken" and "NetPrice" as Text in your database. Either change their datatype to Number or try changing them to Number in the expression.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  5. #5

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163
    I have modify my Access Database file. I changed the datatype from "Text" to "Number", and it is working fine now.

    Can I do it in another way? where the datatype in Access Database file remain as "Text", and change the datatype through coding in VB?

    In VB, I try to add this line of code:

    Code:
    DistributeDataSet.Tables(0).Columns("TotalPrice").DataType = System.Double
    But the last part "System.Double" seems to generate error:

    'Double' is a type in 'System' and cannot be used as an expression.

    How to correct this code?

    I want to use this method because, my DataGrid shows those number like 89.50 as 89.5, 95.00 as 95. I want it to be exactly 2 decimal point all the time. And I do not want to change the structure of my Access Database file.

    Please guide. Thank you.

  6. #6
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    To overcome that error this code:
    VB Code:
    1. DistributeDataSet.Tables(0).Columns("TotalPrice").DataType = GetType(System.Double)
    That changes the datatype of your dataset and you can have the database type remained as text. But I am not sure that wont cause you a problem.
    Try modifying your expression. Some functions are valid in the expression. Try to see if CInt, or Cdbl are valid in the expression or not.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  7. #7

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163
    i think i will change the datatype in Access Database file. because my database has data in it. and VB .NET does not allow me to change via VB coding. anyway thanks for your guidance.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width