Results 1 to 4 of 4

Thread: [RESOLVED] Perform calculation after user input on DataGridView

  1. #1

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Resolved [RESOLVED] Perform calculation after user input on DataGridView

    Hi guys

    I have DataTable, and is bound to DataGridView. So, when the user inputs some values in 4 columns, it would calculate the sum and put it in the last 2nd last column.

    Here's the code that I'm experimenting:
    vb.net Code:
    1. Private Sub DataGridView1_RowLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
    2.  
    3.         Dim i As Integer
    4.         Dim intTotal As Integer = 0
    5.         For i = 5 To DataGridView1.Columns.Count - 3
    6.  
    7.             If Not DataGridView1.Item(e.RowIndex, i).FormattedValue = Nothing Then intTotal += Integer.Parse(DataGridView1.Item(e.RowIndex, i).FormattedValue.ToString)
    8.         Next
    9.         dt.Rows(e.RowIndex).Item("Total") = intTotal
    10.         'DataGridView1.Item(e.RowIndex, DataGridView1.Columns.Count - 2).FormattedValue = intTotal
    11.         'MessageBox.Show(e.RowIndex)
    12.     End Sub
    But when the form is loaded, it would give an error saying out of bounds.

    So, basically it should sum up certain columns when the user enters numbers into it and then display it in the last column.

    I think, there would be a better way to do this. If you have some suggestions, let me know.

    Thanks

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,512

    Re: Perform calculation after user input on DataGridView

    How about using "CellendEdit" instead, heres one I use
    Code:
        Private Sub LOT_CHRGDataGridView_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles LOT_CHRGDataGridView.CellEndEdit
            If e.ColumnIndex = 1 Then
                Me.LOT_CHRGDataGridView.CurrentRow.Cells("tot_amount").Value = CLng(Me.LOT_CHRGDataGridView.CurrentRow.Cells("lbs").Value) / _
                CDbl(Me.LOT_CHRGDataGridView.CurrentRow.Cells("divide_by").Value) * CDbl(Me.LOT_CHRGDataGridView.CurrentRow.Cells("amount").Value)
            End If
        End Sub

  3. #3
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Perform calculation after user input on DataGridView

    The problem is to do with the way you are going through the loop. you are trying to access something that does not exist.

    you need to find out where the error is coming from.... and see how many elements are in the array, and what you are currently trying to read.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Perform calculation after user input on DataGridView

    Thanks guys

    It was my mistake. I used RowIndex in place of ColumnIndex and vice versa(in my code).
    I have changed it and added the code in CellEndEdit event.

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

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