Results 1 to 6 of 6

Thread: [RESOLVED] Aggregate a datagridview column

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2009
    Location
    Ontario, Canada
    Posts
    54

    Resolved [RESOLVED] Aggregate a datagridview column

    Please Help. I am trying to aggregate a column in a datagridview. I know how to get it to work if I bind a database to it, I have several of them working perfectly, but I have one datagrid that is not bound to a database and for the life of me can not fiqure out the Linq code (if there is one). If you can help, I would appreciate it.

    As an example, I have three columns. Product, Qty, Unit Price. I would like it to Multiply the Unit price with the Qty and sum it up in a textbox.

    Thanks,

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Aggregate a datagridview column

    try this:

    vb Code:
    1. Private Sub DataGridView1_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
    2.     TextBox1.Text = (CDec(DataGridView1.Rows(e.RowIndex).Cells("Qty").Value) * CDec(DataGridView1.Rows(e.RowIndex).Cells("Unit_Price").Value)).ToString("C2")
    3. End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2009
    Location
    Ontario, Canada
    Posts
    54

    Re: Aggregate a datagridview column

    Thanks Paul, I will try that as soon as I get home. I post my results.

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 2009
    Location
    Ontario, Canada
    Posts
    54

    Re: Aggregate a datagridview column

    Hi Paul,
    Your code worked but it tallies only a single row, so if I have a total of 5 rows all with different qty and price it doesn't give me a grand total, only displays the total of the selected row. I appreciate your help, I am alot further than I ever would of been. I am now trying to minipulate your code to give a grand total. If you have any suggestions, that would be great.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Aggregate a datagridview column

    vb Code:
    1. Private Sub DataGridView1_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
    2.     TextBox1.Text = (From row As DataGridViewRow In DataGridView1.Rows _
    3.                      Where Not row.IsNewRow _
    4.                      Select (CDec(row.Cells("Qty").Value) * CDec(row.Cells("Unit_Price").Value))).Sum.ToString("C2")
    5. End Sub

  6. #6

    Thread Starter
    Member
    Join Date
    Dec 2009
    Location
    Ontario, Canada
    Posts
    54

    Re: Aggregate a datagridview column

    Hi Paul

    It works perfecty, I can't say thanks enough. I can stop banging my head against the wall, at least for now.

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
  •  



Click Here to Expand Forum to Full Width