|
-
Aug 29th, 2011, 06:27 AM
#1
Thread Starter
Member
[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,
-
Aug 29th, 2011, 01:00 PM
#2
Re: Aggregate a datagridview column
try this:
vb Code:
Private Sub DataGridView1_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
TextBox1.Text = (CDec(DataGridView1.Rows(e.RowIndex).Cells("Qty").Value) * CDec(DataGridView1.Rows(e.RowIndex).Cells("Unit_Price").Value)).ToString("C2")
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 29th, 2011, 01:43 PM
#3
Thread Starter
Member
Re: Aggregate a datagridview column
Thanks Paul, I will try that as soon as I get home. I post my results.
-
Aug 29th, 2011, 05:08 PM
#4
Thread Starter
Member
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.
-
Aug 30th, 2011, 04:21 AM
#5
Re: Aggregate a datagridview column
vb Code:
Private Sub DataGridView1_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
TextBox1.Text = (From row As DataGridViewRow In DataGridView1.Rows _
Where Not row.IsNewRow _
Select (CDec(row.Cells("Qty").Value) * CDec(row.Cells("Unit_Price").Value))).Sum.ToString("C2")
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 30th, 2011, 11:17 AM
#6
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|