Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts,


How can i possibly add every row values for two column then the result could be in the next or third column in datagridview. vb.net

Thank you in advance,

Raz
Posted
Comments
Animesh Datta 9-Dec-15 1:13am    
what have u tried ?
craft_trone 9-Dec-15 2:43am    
my code only add all rows in a column, all i want is, add only a row from two columns.
MayurDighe 9-Dec-15 15:36pm    
show your code...
craft_trone 9-Dec-15 20:40pm    
this is my code @MayurDighe:


Private Sub LoadForPayroll()
Dim totalAmount As Double
Try
sqL = "SELECT remittance_no, remit_date, messenger, item, item_value, rate.product, rate.product_value FROM remittance INNER JOIN rate ON rate.product=remittance.item where rate.ratecode =@rcde and remittance.messenger =@msgr and remit_date between @dfrom and @dto"
ConnDB()
cmd = New MySqlCommand(sqL, conn)
cmd.Parameters.AddWithValue("@rcde", txtRateCode.Text)
cmd.Parameters.AddWithValue("@msgr", txtMessenger.Text)
cmd.Parameters.AddWithValue("@dfrom", dtFrom.Value.Date)
cmd.Parameters.AddWithValue("@dto", dtTo.Value.Date)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
dgw.Rows.Clear()
totalAmount = 0
Do While dr.Read = True
dgw.Rows.Add(dr("remittance_no"), dr("remit_date"), dr("messenger"), dr("item"), dr("item_value"), dr("product_value"))
totalAmount += (dr("product_value") * dr("item_value"))
Loop
lblTotal.Text = Format(totalAmount, "#,##0.00")
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
conn.Close()
End Try
End Sub
craft_trone 9-Dec-15 20:21pm    
this is my code @MayurDighe:


Private Sub LoadForPayroll()
Dim totalAmount As Double
Try
sqL = "SELECT remittance_no, remit_date, messenger, item, item_value, rate.product, rate.product_value FROM remittance INNER JOIN rate ON rate.product=remittance.item where rate.ratecode =@rcde and remittance.messenger =@msgr and remit_date between @dfrom and @dto"
ConnDB()
cmd = New MySqlCommand(sqL, conn)
cmd.Parameters.AddWithValue("@rcde", txtRateCode.Text)
cmd.Parameters.AddWithValue("@msgr", txtMessenger.Text)
cmd.Parameters.AddWithValue("@dfrom", dtFrom.Value.Date)
cmd.Parameters.AddWithValue("@dto", dtTo.Value.Date)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
dgw.Rows.Clear()
totalAmount = 0
Do While dr.Read = True
dgw.Rows.Add(dr("remittance_no"), dr("remit_date"), dr("messenger"), dr("item"), dr("item_value"), dr("product_value"))
totalAmount += (dr("product_value") * dr("item_value"))
Loop
lblTotal.Text = Format(totalAmount, "#,##0.00")
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
conn.Close()
End Try
End Sub

1 solution

Thank you for clarifying the requirement.

If you want the additional data then you can do the calculation in the sql rather than within the gridview i.e.

VB
sqL = "SELECT remittance_no, remit_date, messenger, item, item_value, rate.product, rate.product_value, rate.product_value * item_value as total_value FROM remittance INNER JOIN rate ON rate.product=remittance.item where rate.ratecode =@rcde and remittance.messenger =@msgr and remit_date between @dfrom and @dto"
and then
VB
dgw.Rows.Add(dr("remittance_no"), dr("remit_date"), dr("messenger"), dr("item"), dr("item_value"), dr("product_value"), dr("total_value"))
 'not required now totalAmount += (dr("product_value") * dr("item_value"))

Note that instead of the loop you can populate the DataGridView semi-directly from the reader using a DataTable :
C#
Dim dt = new DataTable()
dt.Load(dr)
dgw.AutoGenerateColumns = True
dgw.DataSource = dt
dgw.Refresh()
(credit[^])
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900