[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,
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
Re: Aggregate a datagridview column
Thanks Paul, I will try that as soon as I get home. I post my results.
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.
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
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.