Click here to Skip to main content
15,906,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good afternoon experts, i just want to ask if anyone knows how to calculate totalamount in datagridview.this is how it looks:

Stocks Item Description Price Sold Quantity
4 COke 5 2 i am the one who will input in this cell


the first three columns are displayed by sql query and i want to know how can i add a new column next to the PRICE?. BTW, the result of the computation of price and sold quantity will be displayed at my textbox. thanks!
Posted

1 solution

Hi,

1. Table structure

ID Item Quantity Rate Remark
1 Coke 20 35 Nothing
2 Chips 12 10 Nothing
3 Item1 45 40 Nothing
4 Item2 60 78 Nothing
5 Item3 40 112 Nothing

2. Lets Bind it in a grid, add a new column name Total next to Rate column, calculate Total = quantity * rate.

3. To bind the grid use datatable as

VB
Dim conn As New OleDb.OleDbConnection
Dim da As New OleDb.OleDbDataAdapter
Dim dt As New DataTable

da = New OleDb.OleDbDataAdapter("Select * from Temp", conn)
da.Fill(dt)


3. Add a new column next to Rate column as -

VB
Dim cl As New DataColumn
        cl = dt.Columns.Add("Total")
        cl.SetOrdinal(4)


4. Calculation Part

VB
Dim Rate As Integer
        Dim Quanity As Integer
        Dim Total As Integer

        For i As Integer = 0 To dt.Rows.Count - 1
            Quanity = CInt(dt.Rows(i)(2))
            Rate = CInt(dt.Rows(i)(3))
            Total = Quanity * Rate

            dt.Rows(i)(4) = Total
        Next


5. Data binding

DataGridView1.DataSource = dt

Done.
 
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