[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 898
  • Last Modified:

Datagrid view calculations

Hello  i have a datagrid view  iwant to make the calculate  for each row.I  have quantity  and unit price (the logic is to multiply quantity  with the unit price) but i dont know who to write it down to  vb and where.

Also i need to calculate total rows sum
can anyone help me pls
0
demoniumz
Asked:
demoniumz
  • 79
  • 38
  • 4
  • +1
1 Solution
 
srikanthreddyn143Commented:
How you want that. Do you have to calculate when they check any check box or what?Where you have to show that total amount and Quantity * price?
0
 
jppintoCommented:
Cells(3) in on DataGridView will be the Total, Cells(1) will be the qty and Cells(2) will be the Unit price.

Then you can put a TextBox1 on your form to display the Total from column 3.

jppinto
Dim sum As Decimal = 0
For Each row As DataGridViewRow In DataGridView1.Rows
   row.Cells(3).Value = row.Cells(1).Value * row.Cells(2).Value
   sum += row.Cells(3).Value   
Next
Me.txtBox1.Text = sum

Open in new window

0
 
demoniumzAuthor Commented:
jppinto nice example what about to display it inside the datagrid view in column total
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
demoniumzAuthor Commented:
how to make that
0
 
jppintoCommented:
The Total for each row is showned inside the DataGridView. I don't think that is correct to display the total column inside the DataGridView.

jppinto
0
 
demoniumzAuthor Commented:
why you tell these
0
 
jppintoCommented:
It's not "normal" coding putting the column total inside the grid...
0
 
demoniumzAuthor Commented:
oups i mean about row total inside the datagrid view no column total.apologize
0
 
demoniumzAuthor Commented:
example

Order id  Product id   Quantity     unitprice         Total
  1              1                 5                 4.20            21.00      One line of the datagridview  give these result

You understan what iwantfirst
0
 
demoniumzAuthor Commented:
anyone to help
0
 
jppintoCommented:
The code that I provided, assumes that you populate your datagridview with 3 columns, Cells(1) for qty, Cells(2) for unit price and Cells(3) for row Total. It will put the row Total on the 3rd column of each row. If you want to put more columns on your datagridview, just change the cell numbers om my code.

jppinto
0
 
demoniumzAuthor Commented:
ok i  try and i provide you the result in the night thnks forhelp
0
 
demoniumzAuthor Commented:
wheere i must the code in the textbox?
0
 
demoniumzAuthor Commented:
where i must add th code
0
 
demoniumzAuthor Commented:
i try it but error.

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

My code order form is below
ublic Class Norders
 
 
 
   
    Private Sub Norders_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Orders' table. You can move, or remove it, as needed.
        Me.OrdersTableAdapter.Fill(Me.NicolaouDBDataSet.Orders)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.OrdersDetails' table. You can move, or remove it, as needed.
        Me.OrdersDetailsTableAdapter.Fill(Me.NicolaouDBDataSet.OrdersDetails)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Products' table. You can move, or remove it, as needed.
        Me.ProductsTableAdapter.Fill(Me.NicolaouDBDataSet.Products)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Suppliers' table. You can move, or remove it, as needed.
        Me.SuppliersTableAdapter.Fill(Me.NicolaouDBDataSet.Suppliers)
        Dim sum As Decimal = 0
        For Each row As DataGridViewRow In OrdersDetailsDataGridView.Rows
            row.Cells(6).Value = row.Cells(4).Value * row.Cells(5).Value
            sum += row.Cells(6).Value
        Next
        Me.TextBox1.Text = sum
    End Sub
 
 
    Private Sub OrdersBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrdersBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.OrdersBindingSource.EndEdit()
        Me.OrdersTableAdapter.Update(Me.NicolaouDBDataSet.Orders)
 
    End Sub
 
    Private Sub movenext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movenext.Click
        OrdersBindingSource.MoveNext()
    End Sub
 
    Private Sub moveprevius_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles moveprevius.Click
        OrdersBindingSource.MovePrevious()
    End Sub
 
    Private Sub movelast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movelast.Click
        OrdersBindingSource.MoveLast()
    End Sub
 
    Private Sub movefirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movefirst.Click
        OrdersBindingSource.MoveFirst()
    End Sub
 
    Private Sub addrec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addrec.Click
        OrdersBindingSource.AddNew()
 
 
    End Sub
 
    Private Sub delrec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delrec.Click
        OrdersBindingSource.RemoveCurrent()
    End Sub
 
    Private Sub OrdersDetailsBindingSourceBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrdersBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.OrdersDetailsBindingSource.EndEdit()
        Me.OrdersDetailsTableAdapter.Update(Me.NicolaouDBDataSet.OrdersDetails)
 
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
 
        Dim bsource As BindingSource = Me.OrdersDetailsDataGridView.DataSource
        Dim ds As DataSet = DirectCast(bsource.DataSource, DataSet)
 
        Dim dr As DataRow = ds.Tables("OrdersDetails").NewRow
        dr("OrderID") = OrderIDTextBox.Text
        dr("ProductID") = ProductIDComboBox.SelectedValue
        dr("UnitPrice") = UnitPriceTextBox.Text
        dr("Quantity") = QuantityTextBox.Text
        dr("productname") = ProductNameTextBox1.Text
        ds.Tables("OrdersDetails").Rows.Add(dr)
 
    End Sub
 
 
 
    
 
 
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim bsource As BindingSource = Me.OrdersDetailsDataGridView.DataSource
        Dim ds As DataSet = DirectCast(bsource.DataSource, DataSet)
 
        Dim dr As DataRow = ds.Tables("OrdersDetails").NewRow
        dr("OrderID") = OrderIDTextBox.Text
        dr("ProductID") = ProductIDComboBox.SelectedValue
        dr("UnitPrice") = UnitPriceTextBox.Text
        dr("Quantity") = QuantityTextBox.Text
        dr("productname") = ProductNameTextBox1.Text
        ds.Tables("OrdersDetails").Rows.Clear()
    End Sub
 
    Private Sub OrdersDetailsDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles OrdersDetailsDataGridView.CellContentClick
       
    End Sub
End Class

Open in new window

0
 
demoniumzAuthor Commented:
anyone to chelp me to make these to work please??
0
 
srikanthreddyn143Commented:
row.Cells(6).Value = row.Cells(4).Value * row.Cells(5).Value


Are these column indexes are correct?
0
 
demoniumzAuthor Commented:
error corect forget that the cell start  to 0 (0 BASED) BUT THE TOTAL OF EACH ROW AND AND COLUM NOT APPEAR yet
0
 
srikanthreddyn143Commented:
Debug by keeping debug point at for loop or Me.TextBox1.Text = sum and check what is the value in sum.

And Also check whether you are getting right column values
0
 
demoniumzAuthor Commented:
problem resolved at some part but when i add a new product in the datagrid the total  is not appear and the sum not change
.2nd problem  when  a product remove it the sum is not change
 3rd add a new  order  the total is not reset to  0
0
 
srikanthreddyn143Commented:
Keep the whole logic of multiplying and summing to show in text box in seperate function.

When ever you update or remove , call that function so it will update column value and text box total value
0
 
demoniumzAuthor Commented:
can u  help me  to make a function when a product add to   give row total and update the total column
0
 
srikanthreddyn143Commented:
Private Sub Total()
   Dim sum As Decimal = 0
        For Each row As DataGridViewRow In OrdersDetailsDataGridView.Rows
            row.Cells(6).Value = row.Cells(4).Value * row.Cells(5).Value
            sum += row.Cells(6).Value
        Next
        Me.TextBox1.Text = sum
     
End Sub

Where ever you are updating/ adding call Total()
0
 
demoniumzAuthor Commented:
for delete  what i must called ?
0
 
srikanthreddyn143Commented:
same you can call Total()
0
 
demoniumzAuthor Commented:
any site  to provide me information for these type of fuction examples?
0
 
srikanthreddyn143Commented:
You can search in Google for VB.Net tutorials where you ccan find Defining Functions and Subs etc.
Is that working?
0
 
demoniumzAuthor Commented:
nop very much
0
 
demoniumzAuthor Commented:
i dont understand why when i add new product the total  of the row is not appear  but the previous product before the code enter to caslculate thwe sum is appear
0
 
srikanthreddyn143Commented:
grid should be refreshed after you add the product
0
 
demoniumzAuthor Commented:
how?
0
 
srikanthreddyn143Commented:
r u binding data to grid view from designer property?
0
 
demoniumzAuthor Commented:
yes
0
 
srikanthreddyn143Commented:
After you add/update call OrdersDetailsDataGridView.Refresh()
0
 
demoniumzAuthor Commented:
not working
0
 
srikanthreddyn143Commented:
Can you place the whole code here what you had ?
0
 
demoniumzAuthor Commented:
Public Class Norders
 
   
    Private Sub Norders_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Orders' table. You can move, or remove it, as needed.
        Me.OrdersTableAdapter.Fill(Me.NicolaouDBDataSet.Orders)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.OrdersDetails' table. You can move, or remove it, as needed.
        Me.OrdersDetailsTableAdapter.Fill(Me.NicolaouDBDataSet.OrdersDetails)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Products' table. You can move, or remove it, as needed.
        Me.ProductsTableAdapter.Fill(Me.NicolaouDBDataSet.Products)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Suppliers' table. You can move, or remove it, as needed.
        Me.SuppliersTableAdapter.Fill(Me.NicolaouDBDataSet.Suppliers)
        Dim sum As Decimal = 0
        For Each row As DataGridViewRow In OrdersDetailsDataGridView.Rows
            row.Cells(5).Value = row.Cells(3).Value * row.Cells(4).Value
            sum += row.Cells(5).Value
        Next
        Me.TextBox1.Text = sum
    End Sub
 
 
    Private Sub OrdersBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrdersBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.OrdersBindingSource.EndEdit()
        Me.OrdersTableAdapter.Update(Me.NicolaouDBDataSet.Orders)
 
    End Sub
 
    Private Sub movenext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movenext.Click
        OrdersBindingSource.MoveNext()
    End Sub
 
    Private Sub moveprevius_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles moveprevius.Click
        OrdersBindingSource.MovePrevious()
    End Sub
 
    Private Sub movelast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movelast.Click
        OrdersBindingSource.MoveLast()
    End Sub
 
    Private Sub movefirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movefirst.Click
        OrdersBindingSource.MoveFirst()
    End Sub
 
    Private Sub addrec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addrec.Click
        OrdersBindingSource.AddNew()
 
 
    End Sub
 
    Private Sub delrec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delrec.Click
        OrdersBindingSource.RemoveCurrent()
    End Sub
 
    Private Sub OrdersDetailsBindingSourceBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrdersBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.OrdersDetailsBindingSource.EndEdit()
        Me.OrdersDetailsTableAdapter.Update(Me.NicolaouDBDataSet.OrdersDetails)
       
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
 
        Dim bsource As BindingSource = Me.OrdersDetailsDataGridView.DataSource
        Dim ds As DataSet = DirectCast(bsource.DataSource, DataSet)
 
        Dim dr As DataRow = ds.Tables("OrdersDetails").NewRow
        dr("OrderID") = OrderIDTextBox.Text
        dr("ProductID") = ProductIDComboBox.SelectedValue
        dr("UnitPrice") = UnitPriceTextBox.Text
        dr("Quantity") = QuantityTextBox.Text
        dr("productname") = ProductNameTextBox1.Text
 
        ds.Tables("OrdersDetails").Rows.Add(dr)
 
    End Sub
 
 
 
   
 
 
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim bsource As BindingSource = Me.OrdersDetailsDataGridView.DataSource
        Dim ds As DataSet = DirectCast(bsource.DataSource, DataSet)
 
        Dim dr As DataRow = ds.Tables("OrdersDetails").NewRow
        dr("OrderID") = OrderIDTextBox.Text
        dr("ProductID") = ProductIDComboBox.SelectedValue
        dr("UnitPrice") = UnitPriceTextBox.Text
        dr("Quantity") = QuantityTextBox.Text
        dr("productname") = ProductNameTextBox1.Text
 
        ds.Tables("OrdersDetails").Rows.Clear()
    End Sub
 
    Private Sub OrdersDetailsDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles OrdersDetailsDataGridView.CellContentClick
     
    End Sub
 
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
       
    End Sub
End Class
0
 
demoniumzAuthor Commented:
code is here so?
0
 
srikanthreddyn143Commented:
you didnt seperate the code part as seprate function Total() right?
0
 
srikanthreddyn143Commented:
Try using this
Public Class Norders
 
   
    Private Sub Norders_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Orders' table. You can move, or remove it, as needed.
        Me.OrdersTableAdapter.Fill(Me.NicolaouDBDataSet.Orders)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.OrdersDetails' table. You can move, or remove it, as needed.
        Me.OrdersDetailsTableAdapter.Fill(Me.NicolaouDBDataSet.OrdersDetails)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Products' table. You can move, or remove it, as needed.
        Me.ProductsTableAdapter.Fill(Me.NicolaouDBDataSet.Products)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Suppliers' table. You can move, or remove it, as needed.
        Me.SuppliersTableAdapter.Fill(Me.NicolaouDBDataSet.Suppliers)
        
    End Sub
 
 
    Private Sub OrdersBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrdersBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.OrdersBindingSource.EndEdit()
        Me.OrdersTableAdapter.Update(Me.NicolaouDBDataSet.Orders)
 OrdersDetailsDataGridView.Refresh()
 Total()
    End Sub
 
    Private Sub movenext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movenext.Click
        OrdersBindingSource.MoveNext()
    End Sub
 
    Private Sub moveprevius_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles moveprevius.Click
        OrdersBindingSource.MovePrevious()
    End Sub
 
    Private Sub movelast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movelast.Click
        OrdersBindingSource.MoveLast()
    End Sub
 
    Private Sub movefirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movefirst.Click
        OrdersBindingSource.MoveFirst()
    End Sub
 
    Private Sub addrec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addrec.Click
        OrdersBindingSource.AddNew()
 
 
    End Sub
 
    Private Sub delrec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delrec.Click
        OrdersBindingSource.RemoveCurrent()
    End Sub
 
    Private Sub OrdersDetailsBindingSourceBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrdersBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.OrdersDetailsBindingSource.EndEdit()
        Me.OrdersDetailsTableAdapter.Update(Me.NicolaouDBDataSet.OrdersDetails)
       OrdersDetailsDataGridView.Refresh()
	   Total()
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
 
        Dim bsource As BindingSource = Me.OrdersDetailsDataGridView.DataSource
        Dim ds As DataSet = DirectCast(bsource.DataSource, DataSet)
 
        Dim dr As DataRow = ds.Tables("OrdersDetails").NewRow
        dr("OrderID") = OrderIDTextBox.Text
        dr("ProductID") = ProductIDComboBox.SelectedValue
        dr("UnitPrice") = UnitPriceTextBox.Text
        dr("Quantity") = QuantityTextBox.Text
        dr("productname") = ProductNameTextBox1.Text
 
        ds.Tables("OrdersDetails").Rows.Add(dr)
 
    End Sub
 
 
 
   
 
 
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim bsource As BindingSource = Me.OrdersDetailsDataGridView.DataSource
        Dim ds As DataSet = DirectCast(bsource.DataSource, DataSet)
 
        Dim dr As DataRow = ds.Tables("OrdersDetails").NewRow
        dr("OrderID") = OrderIDTextBox.Text
        dr("ProductID") = ProductIDComboBox.SelectedValue
        dr("UnitPrice") = UnitPriceTextBox.Text
        dr("Quantity") = QuantityTextBox.Text
        dr("productname") = ProductNameTextBox1.Text
 
        ds.Tables("OrdersDetails").Rows.Clear()
    End Sub
 
    Private Sub OrdersDetailsDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles OrdersDetailsDataGridView.CellContentClick
     
    End Sub
 
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
       
    End Sub
 
	Private Sub Total()
    Dim sum As Decimal = 0
        For Each row As DataGridViewRow In OrdersDetailsDataGridView.Rows
            row.Cells(5).Value = row.Cells(3).Value * row.Cells(4).Value
            sum += row.Cells(5).Value
        Next
        Me.TextBox1.Text = sum
	End Sub
End Class

Open in new window

0
 
demoniumzAuthor Commented:
                          Description                                       Line            column
Error      2      Property access must assign to the property or use its value.      D:\Nicolaou\Nicolaou\Norders.vb      23      9      Nicolaou
Error      3      Property access must assign to the property or use its value.      D:\Nicolaou\Nicolaou\Norders.vb      57      9      Nicolaou
Error      4      'Total' is already declared as 'Friend Dim WithEvents Total As System.Windows.Forms.DataGridViewTextBoxColumn' in this class.      D:\Nicolaou\Nicolaou\Norders.vb      102      17      Nicolaou
0
 
demoniumzAuthor Commented:
TOTAL make  problem  see above the errors
0
 
srikanthreddyn143Commented:
Rename that total() AS TotalSum() and try it
0
 
demoniumzAuthor Commented:
Input string was not in a correct format.Couldn't store <> in OrderID Column.  Expected type is Int32.

  dr("OrderID") = OrderIDTextBox.Text
0
 
srikanthreddyn143Commented:
dr("OrderID") = Convert.ToInt32(OrderIDTextBox.Text)
0
 
demoniumzAuthor Commented:
working better but tootal row and column not display
0
 
demoniumzAuthor Commented:
can u help
0
 
srikanthreddyn143Commented:
Is that Total column should only be visible on grid  or does it should go to database?
0
 
demoniumzAuthor Commented:
TOtal column  goes to  database
0
 
srikanthreddyn143Commented:
Is that total column avlbl in your binding source? If yes then in the Cells, check the correct index. Debug the code, and check what is the index of unit price, quantity and total.
0
 
demoniumzAuthor Commented:
how to check if is available in my binding source?
0
 
srikanthreddyn143Commented:
Go to binding source properties and look for the column collection
0
 
demoniumzAuthor Commented:
i dont find it
0
 
srikanthreddyn143Commented:
At the button4_click, just keep a break point and try to debug and say what are the columns in Datagridview Datasource. What is the binding sourse for your datagridview?
0
 
demoniumzAuthor Commented:
ii thinks is beeter to update the aplication  so you cabn help can u>?
0
 
srikanthreddyn143Commented:
ya sure. The thing is for the datagridview, where you are giving datasourse.Is it in the design properties.If Yes, what is the source name?
0
 
demoniumzAuthor Commented:
http://uploading.com/files/AIWEX8AD/Nicolaou.zip.html  (my aplication)
and my database can u help me pls i am stack here now a lot of days

i upload it for beter understand i am  new to vb 2005 so  maybe i dont know thing so pls open it a llook orders form
NicolaouDB.mdb
0
 
demoniumzAuthor Commented:
can someone tell me how to clear my rows in the datagrid view i have a button  but is not work
0
 
demoniumzAuthor Commented:
???????
0
 
demoniumzAuthor Commented:
Operator '+' is not defined for type 'Decimal' and type 'DBNull'.

Dim sum As Decimal = 0
        For Each row As DataGridViewRow In OrdersDetailsDataGridView.Rows
            row.Cells(5).Value = row.Cells(3).Value * row.Cells(4).Value
            sum += row.Cells(5).Value
        Next
        Me.TextBox1.Text = sum

what is these can some corect it  or provide me a new calculate code?
0
 
srikanthreddyn143Commented:
In row.Cells(5).Value, you dont have any value. If you want to clear  datagridview, use datagridview.datasource = nothing
0
 
demoniumzAuthor Commented:
In row.Cells(5).Value, you dont have any value what are u mean
0
 
srikanthreddyn143Commented:
I made some changes to the code i will post it. But there is some problem in your code which is creating some issues.
Public Class Norders
    Private im As Object
 
    Private Sub Norders_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Orders' table. You can move, or remove it, as needed.
        Me.OrdersTableAdapter.Fill(Me.NicolaouDBDataSet.Orders)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.OrdersDetails' table. You can move, or remove it, as needed.
        Me.OrdersDetailsTableAdapter.Fill(Me.NicolaouDBDataSet.OrdersDetails)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Products' table. You can move, or remove it, as needed.
        Me.ProductsTableAdapter.Fill(Me.NicolaouDBDataSet.Products)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Suppliers' table. You can move, or remove it, as needed.
        Me.SuppliersTableAdapter.Fill(Me.NicolaouDBDataSet.Suppliers)
 
        TotalSum()
    
    End Sub
 
 
   
 
 
    Private Sub OrdersBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrdersBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.OrdersBindingSource.EndEdit()
        Me.OrdersTableAdapter.Update(Me.NicolaouDBDataSet.Orders)
    End Sub
 
    Private Sub movenext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movenext.Click
        OrdersBindingSource.MoveNext()
    End Sub
 
    Private Sub moveprevius_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles moveprevius.Click
        OrdersBindingSource.MovePrevious()
    End Sub
 
    Private Sub movelast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movelast.Click
        OrdersBindingSource.MoveLast()
    End Sub
 
    Private Sub movefirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movefirst.Click
        OrdersBindingSource.MoveFirst()
    End Sub
 
    Private Sub addrec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addrec.Click
        OrdersBindingSource.AddNew()
 
 
    End Sub
 
    Private Sub delrec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delrec.Click
        OrdersBindingSource.RemoveCurrent()
    End Sub
 
    Private Sub OrdersDetailsBindingSourceBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrdersBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.OrdersDetailsBindingSource.EndEdit()
        Me.OrdersDetailsTableAdapter.Update(Me.NicolaouDBDataSet.OrdersDetails)
        Me.OrdersDetailsDataGridView.Refresh()
        TotalSum()
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
 
        Dim bsource As BindingSource = Me.OrdersDetailsDataGridView.DataSource
        Dim ds As DataSet = DirectCast(bsource.DataSource, DataSet)
        Dim orderdetailstable As New DataTable
        Dim dr As DataRow = ds.Tables("OrdersDetails").NewRow
        dr("OrderID") = OrderIDTextBox.Text
        dr("ProductID") = ProductIDComboBox.SelectedValue
        dr("UnitPrice") = UnitPriceTextBox.Text
        dr("Quantity") = QuantityTextBox.Text
        dr("productname") = ProductNameTextBox1.Text
        dr("Discount") = 0
        dr("Total records") = 0
        dr("Total") = Convert.ToDecimal(UnitPriceTextBox.Text.ToString()) * Convert.ToDecimal(QuantityTextBox.Text.ToString())
 
        ds.Tables("OrdersDetails").Rows.Add(dr)
 
        TotalSum()
        
    End Sub
 
 
 
 
 
 
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim bsource As BindingSource = Me.OrdersDetailsDataGridView.DataSource
        Dim ds As DataSet = DirectCast(bsource.DataSource, DataSet)
        Dim orderdetailstable As New DataTable
        Dim dr As DataRow = ds.Tables("OrdersDetails").NewRow
        dr("OrderID") = OrderIDTextBox.Text
        dr("ProductID") = ProductIDComboBox.SelectedValue
        dr("UnitPrice") = UnitPriceTextBox.Text
        dr("Quantity") = QuantityTextBox.Text
        dr("productname") = ProductNameTextBox1.Text
        dr("Discount") = 0
        dr("Total records") = 0
        dr("Total") = Convert.ToDecimal(UnitPriceTextBox.Text.ToString()) * Convert.ToDecimal(QuantityTextBox.Text.ToString())
        ds.Tables("OrdersDetails").Rows.Clear()
 
    End Sub
 
    Private Sub OrdersDetailsDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles OrdersDetailsDataGridView.CellContentClick
 
    End Sub
 
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
 
    End Sub
 
    Private Sub OrderIDDataGridViewTextBoxColumn_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles OrderIDDataGridViewTextBoxColumn.Disposed
 
    End Sub
 
    'Private Sub OrdersDetailsDataGridView_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles OrdersDetailsDataGridView.RowsAdded
    '    Dim sum As Decimal = 0
    '    For Each row As DataGridViewRow In OrdersDetailsDataGridView.Rows
    '        row.Cells(5).Value = row.Cells(3).Value * row.Cells(4).Value
    '        sum += row.Cells(5).Value
    '    Next
    '    Me.TextBox1.Text = sum
 
 
    'End Sub
 
    Private Sub TotalSum()
        Dim sum As Decimal = 0
        For Each row As DataGridViewRow In OrdersDetailsDataGridView.Rows
            row.Cells(5).Value = row.Cells(3).Value * row.Cells(4).Value
            sum += row.Cells(5).Value
        Next
        Me.TextBox1.Text = sum
    End Sub
End Class

Open in new window

0
 
demoniumzAuthor Commented:
what a re u mean  what problems?
0
 
srikanthreddyn143Commented:
When ever i was trying to insert a new record it was overroding the first row. Can u try the application now?
0
 
demoniumzAuthor Commented:
nop yet  is working for you? all ok or no?
0
 
srikanthreddyn143Commented:
Total column is working fine and text box is showing correct .But insert is not working fine. Can you check it?
0
 
demoniumzAuthor Commented:
insert is working excelent for me now  i remake the form  also  Quantity is now ok the only problem except  total  i have(havent yet  try your code ) is the clear code  the   datagrid view is clear but the save not update the datagrid view  so the next time i load the form i see again  tghe data as before

 OrdersDetailsDataGridView.DataSource = Nothing  cauze the aplication  can u  help me to make a  button to clear all the  fields and save it (using biding navigator  save button)

0
 
demoniumzAuthor Commented:
?????????????
0
 
srikanthreddyn143Commented:
What are all you want to clear.Do yu want to clear dataset also?
0
 
srikanthreddyn143Commented:
When you want to clear everything then

Me.NicolaouDBDataSet.OrdersDetails = Nothing
0
 
demoniumzAuthor Commented:
to clear dataset you mean when add new order to clean the datagridview?
0
 
demoniumzAuthor Commented:
Error      1      Property 'OrdersDetails' is 'ReadOnly'.      D:\Nicolaou\Nicolaou\Norder.vb      63      9      Nicolaou


Whattttttt????:) what is these
0
 
demoniumzAuthor Commented:
Me.NicolaouDBDataSet.OrdersDetails = Nothing provide error
0
 
srikanthreddyn143Commented:
Sorry try this
Me.NicolaouDBDataSet.OrdersDetails.Clear(). When you want to clear the dataset?
0
 
demoniumzAuthor Commented:
Excelent work excelent  programer  :) the problems remain   in some part when a product delete de product is deleted normally but the toal is remain the same
0
 
srikanthreddyn143Commented:
Call TotalSum() At the delete portion of code after deletion is done
0
 
demoniumzAuthor Commented:
want to clear the dataset when  add a new  order form  (add binding navigator +(plus)  button  to clear the datagrid view
0
 
srikanthreddyn143Commented:
You mean to say Total in the Textbox remains same? If yes make TextBox.Text = String.Empty
0
 
demoniumzAuthor Commented:
the problems remain   in some part when a product delete de product is deleted normally but the toal is remain the same  

Problem resolved

0
 
srikanthreddyn143Commented:
Is that fine?
0
 
demoniumzAuthor Commented:
absolutely  yea 3  morre small problems: :: clear button clear the form  but the save button not save the changes when close and reload aplication the  data that is cleared before is exist in the datagrid view

And the second problem when a total row and total column is appear like 119 lets say  i want to display as (the symbol of euro ) 119.00

Third   when  add a new order form (push the add button in the binding navigator to clear the form  so i can eddit it again
0
 
demoniumzAuthor Commented:
cfan u help me pls on that or  i  must open  new questions
0
 
demoniumzAuthor Commented:
srikanthreddyn143 are u here?
0
 
srikanthreddyn143Commented:
For the 2nd Problem use this snippet in load

OrdersDetailsDataGridView.Columns(5).DefaultCellStyle.Format = "$0.00"
Instead of dollar sign use Euro Sign

For1st prob, after you clear it and save make sure the data is getting into database.

For 3rd prob, When you click on add button, before you call OrdersBindingSource.AddNew()

Make Me.NicolaouDBDataSet.OrdersDetails.Clear()
Textbxtotal.Text = String.Empty
0
 
demoniumzAuthor Commented:
For the 2nd Problem use this snippet in load

OrdersDetailsDataGridView.Columns(5).DefaultCellStyle.Format = "$0.00"
Instead of dollar sign use Euro Sign
where i must write these?
0
 
demoniumzAuthor Commented:
Textbxtotal.Text = String.Empty (these Textbxtotal is the column total?Textbxtotal.Text = String.Empty
0
 
demoniumzAuthor Commented:
Problem 1 and problem 3 resolved
0
 
demoniumzAuthor Commented:
oups ok in  load wait now to find the Euro symbol
0
 
demoniumzAuthor Commented:
how i can  set it to  total column Textbox1.Text
0
 
demoniumzAuthor Commented:
how i can  set it to  total  Textbox1.Text to  be like these "¬ 0.00"
0
 
demoniumzAuthor Commented:
how i can  set it to  total  Textbox1.Text to  be like these " ¬0.00"?
0
 
demoniumzAuthor Commented:
srikanthreddyn143:.........
0
 
demoniumzAuthor Commented:
The problem now and  the last one that remain is the Comun total (textbox1) is not get value like the row $0.00 but remain 000 can u help  
0
 
demoniumzAuthor Commented:
??????
0
 
srikanthreddyn143Commented:
sorry i was not at desk.

use this Me.TextBox1.Text = sum.ToString("$#,###.00")

to Clear text box use this

       TextBox1.Text = "$0.00"

For total column format in gridview you can try this also
OrdersDetailsDataGridView.Columns(5).DefaultCellStyle.Format = "$#,###.00"


0
 
demoniumzAuthor Commented:
Name sum is not decleare why?
0
 
demoniumzAuthor Commented:
hello are u here?
0
 
srikanthreddyn143Commented:
Me.TextBox1.Text = sum.ToString("$#,###.00")
use this in TotalSum().dnt use it in load
0
 
demoniumzAuthor Commented:
where i must use it?
0
 
demoniumzAuthor Commented:
problem  why ehen in the form  lets say there is a product Quantity 8 and amount 1.66 give 8 euro
 the real result is 8.30 euro why that ?
0
 
demoniumzAuthor Commented:
Total sum is fixed need to fix amount (above problem)
0
 
demoniumzAuthor Commented:
no one know?
0
 
demoniumzAuthor Commented:
something i  forget to mentioned needed:Iwant when a suplier  clicked Lets say  demoniumz Suplier company "KEO LTD" TO SEE ONLY THE "KEO" PRODUCTS and not all the products

srikanthreddyn143: can u help or anyone else want is welcome

thnks for help apologize
0
 
srikanthreddyn143Commented:
Look whether Total Column in dataset is Decimal or Integer.Let me check and I will let you know.
0
 
demoniumzAuthor Commented:
ok i wait what about th other prpoblem
0
 
demoniumzAuthor Commented:
before is set to long integer now i  put it as decimal or integer  but nothing change  
0
 
demoniumzAuthor Commented:
i dont know  where to see in dataset can u help me can u look?i look in my database access 2003 and is set in long integer but after i  turn to decimal and integer nothing change but chek it maybe i am  wrong
0
 
abelCommented:
This thread seems to be (almost) equal to this one: http://www.experts-exchange.com/Microsoft/Development/Visual_Studio_Express/Q_24354173.html where I try to get the information from the OP "demoniumz" to understand what is actually the problem. The code posted in both threads is the same.

@demoniumz: you also requested attention on this thread. Can you try to be clear on what you actually want to do?

> ok i wait what about th other prpoblem

please ask only one question per thread. See EE guidelines on this.

> hello are u here?

same as in that other thread: remember we are all volunteers. Don't push us, because that will only have the reverse effect.
0
 
demoniumzAuthor Commented:
The previous Question is asked in a new question in order to not ask something again here.No one provide that i need so i ask here the expert to help  if he can.The other is understable.
0
 
demoniumzAuthor Commented:
Anyway the problems are  exist if anyone want to help me with  these two small problem i am  here

Thnks srikanthreddyn143
0
 
srikanthreddyn143Commented:
In Access Database the Total Column datatype is Integer change it to Decimal.

For 2nd Prob
Private Sub SupplierIDComboBox_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SupplierIDComboBox.SelectedValueChanged
        'Load ProductID Combo Box Based on the supplierID
    End Sub
0
 
demoniumzAuthor Commented:
i get the same result is working  for you is give you exactly the amount ? can u  sent me the database fixed Upload it here?
0
 
demoniumzAuthor Commented:
Private Sub SupplierIDComboBox_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SupplierIDComboBox.SelectedValueChanged
        'Load ProductID Combo Box Based on the supplierID
    End Sub

These is comment what is the code?
0
 
srikanthreddyn143Commented:
I dnt have MS Access in my system. I looke dinto it through VS2008.Pls go to MS Access and change the Datatype of that column.

In the commented part ypu have to filter the data using supplierid like this

NicolaouDBDataSet.Products.Select("SUPPLIERID = '" & SupplierIDComboBox.SelectedValue.ToString() & "'")

it s an example


0
 
srikanthreddyn143Commented:
I dnt kno much abt Binding sources
0
 
abelCommented:
> i get the same result is working  for you is give you exactly the amount ?
> can u  sent me the database fixed Upload it here?


demoniumz, I think you are mixing the question threads again. You didn't send the database here, you send it to me. That is why EE doesn't want cross-thread posts of one question, that causes confusion and problems.

We are here to help, and we are all volunteers, I think I already said that earlier. So we can point you in the direction of how to do something, but we are not going to do the job for you.

About Access, you are around here for quite some time, you should know by now how it works, so please ask that question on the Access zone.

-- Abel --
0
 
demoniumzAuthor Commented:
ok abel ok abel
0
 
demoniumzAuthor Commented:
Excelent thnks for help  freind
0
 
demoniumzAuthor Commented:
srikanthreddyn143: need to help me pls can u ?
0
 
srikanthreddyn143Commented:
What is the problem left?
0
 
demoniumzAuthor Commented:
ii forget to add discount  to the  datagridview  and in the calculation
0
 
demoniumzAuthor Commented:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_24376572.html#a24293762 here is my  problem

srikanthreddyn143   i need help pls in order to finish my aplication
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

  • 79
  • 38
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now