• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 531
  • Last Modified:

duplicate value in datagridview

hi  i have a form purchase and i pass a value quantity from a textbox to datagridview  but i get duplicate value as you can see bellow.Bellow is my code i use

The only problem that remain now i fix it  the other part is when i  insert the quantity  in to datagridview the previous rows take the same value lets say

purchase id  product id product name Unitprice quantity  total
1                     2               sain nicolas   $2.40         2    $4.80

when i insert another product with  quantity lets say 5 i get
purchase id  product id product name Unitprice quantity  total
1                     2               sain nicolas   $2.40         5    $4.80
1                      1               malboro light  $3.80         5   $19.00

why these happend bellow is my code


Public Class NicolaouPurchase
 
    Private Sub PurchaseBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PurchaseBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.PurchaseBindingSource.EndEdit()
        Me.PurchaseTableAdapter.Update(Me.NicolaouDBDataSet.Purchase)
 
    End Sub
   
    Private Sub PurchasedetailsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PurchaseBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.PurchasedetailsBindingSource.EndEdit()
        Me.PurchasedetailsTableAdapter.Update(Me.NicolaouDBDataSet.Purchasedetails)
 
    End Sub
    Private Sub NicolaouPurchase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Purchasedetails' table. You can move, or remove it, as needed.
        Me.PurchasedetailsTableAdapter.Fill(Me.NicolaouDBDataSet.Purchasedetails)
        '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)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Purchase' table. You can move, or remove it, as needed.
        Me.PurchaseTableAdapter.Fill(Me.NicolaouDBDataSet.Purchase)
 
    End Sub
 
    Private Sub PurchaseBindingNavigator_RefreshItems(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PurchaseBindingNavigator.RefreshItems
 
    End Sub
 
    Private Sub Addpr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Addpr.Click
        Dim bsource As BindingSource = Me.PurchasedetailsDataGridView.DataSource
        Dim ds As DataSet = DirectCast(bsource.DataSource, DataSet)
        Dim orderdetailstable As New DataTable
        Dim dr As DataRow = ds.Tables("PurchaseDetails").NewRow
        dr("Purchaseid") = PurchaseIDTextBox.Text
        dr("Productid") = ProductIDComboBox1.SelectedValue
        dr("UnitPrice") = UnitPriceTextBox1.Text
        dr("Quantity") = QuantityTextBox.Text
        dr("Productname") = ProductNameTextBox1.Text
        dr("Total") = Convert.ToString(UnitPriceTextBox1.Text.ToString()) * Convert.ToString(QuantityTextBox.Text.ToString())
 
        ds.Tables("PurchaseDetails").Rows.Add(dr)
 
        TotalSum()
    End Sub
    Private Sub TotalSum()
        Dim sum As Decimal = 0
        For Each row As DataGridViewRow In PurchasedetailsDataGridView.Rows
            row.Cells(5).Value = row.Cells(3).Value * CDec(row.Cells(4).Value)
            sum += row.Cells(5).Value
        Next
        Me.TextBox1.Text = sum
        Me.TextBox1.Text = sum.ToString("$#,###.##")
 
        PurchasedetailsDataGridView.Columns(5).DefaultCellStyle.Format = ("$#,###.##")
 
    End Sub
 
    Private Sub PurchasedetailsDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles PurchasedetailsDataGridView.CellContentClick
 
    End Sub
 
    Private Sub addrec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addrec.Click
        Me.NicolaouDBDataSet.Purchasedetails.Clear()
        TextBox1.Text = String.Empty
        PurchaseBindingSource.AddNew()
    End Sub
End Class

Open in new window

0
demoniumz
Asked:
demoniumz
  • 23
  • 8
  • 4
2 Solutions
 
DeathraceCommented:
let me know,, does the below code works for you...i feel it should..
 Private Sub TotalSum()
        Dim sum As Decimal = 0
        For Each row As DataGridViewRow In PurchasedetailsDataGridView.Rows
            int cell3value = Convert.toInt32(row.Cells(3).Text);
            int cell4value = Convert.toInt32(row.Cells(4).Text);
            row.Cells(5).Value = (cell3value * cell4value);
            sum += row.Cells(5).Value
      End Sub

Open in new window

0
 
CodeCruiserCommented:
I dont understand your problem where you say the previous rows take the same quantity. needs more elaboration there.

You can eliminate the TotalSum method altogether. Use this when adding row

dim TotalAmount as single = Convert.ToString(UnitPriceTextBox1.Text.ToString()) * Convert.ToString(QuantityTextBox.Text.ToString())
GrandTotal += TotalAmount
dr("Total") = TotalAmount
textbox1.text = grandtotal
0
 
demoniumzAuthor Commented:
cell3value cellvalue4 and row is not declare
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
CodeCruiserCommented:
And my suggestion?
0
 
demoniumzAuthor Commented:
dr and grandtotal is not decleare
0
 
demoniumzAuthor Commented:
what is the grandtotal?
0
 
CodeCruiserCommented:
My code would go in the Addpr_Click method. dr is already declared as datarow in that method.
GrandTotal (as it sounds) would be a form level variable of type single or integer to hold to total amount of the order.
0
 
demoniumzAuthor Commented:
grand total is not   declare i dont understand how to fix  it?
0
 
CodeCruiserCommented:
My Goodness. Mate put this statement somewhere

dim GrandTotal as single
0
 
demoniumzAuthor Commented:
lol  now i understand you sorry about this
0
 
demoniumzAuthor Commented:
You see now what i mean  Sage the code is not help
untitled.PNG
0
 
CodeCruiserCommented:
I do not see any reason why its changing the quantity of previous row. Why dont you use a simple DataTable instead of bindingsource and bindingnavigator etc.
0
 
demoniumzAuthor Commented:
that i ask  why is changed if i know i dont ask my freind
0
 
demoniumzAuthor Commented:
i have the same form  for order sam way and is work
0
 
demoniumzAuthor Commented:
The orders form code  is same have a look
Public Class Norder
 
    Private Sub Norder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Purchase' table. You can move, or remove it, as needed.
        Me.PurchaseTableAdapter.Fill(Me.NicolaouDBDataSet.Purchase)
 
 
    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 Addpr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Addpr.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") = ProductNameTextBox.Text
        'dr("Discount") = DiscountTextBox.Text
        dr("Total records") = 0
        dr("Total") = Convert.ToString(Unitpricetextbox.Text.ToString()) * Convert.ToString(QuantityTextBox.Text.ToString())
 
        ds.Tables("OrdersDetails").Rows.Add(dr)
 
        TotalSum()
    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 clearbu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delbut.Click
 
        OrdersDetailsDataGridView.Rows.Remove(OrdersDetailsDataGridView.CurrentRow)
 
        TotalSum()
    End Sub
 
    Private Sub TotalSum()
        Dim sum As Decimal = 0
        For Each row As DataGridViewRow In OrdersDetailsDataGridView.Rows
            row.Cells(6).Value = row.Cells(3).Value * CDec(row.Cells(4).Value)
            sum += row.Cells(6).Value
        Next
        Me.TextBox1.Text = sum
        Me.TextBox1.Text = sum.ToString("$#,###.##")
 
        OrdersDetailsDataGridView.Columns(5).DefaultCellStyle.Format = ("$#,###.##")
 
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearbut.Click
        Me.NicolaouDBDataSet.OrdersDetails.Clear()
    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
        Me.NicolaouDBDataSet.OrdersDetails.Clear()
        TextBox1.Text = String.Empty
        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 ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
      
        Me.ProductsTableAdapter.Fill(Me.NicolaouDBDataSet.Products)
        '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.Suppliers' 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.Customers' table. You can move, or remove it, as needed.
        Me.CustomersTableAdapter.Fill(Me.NicolaouDBDataSet.Customers)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Employees' table. You can move, or remove it, as needed.
        Me.EmployeesTableAdapter.Fill(Me.NicolaouDBDataSet.Employees)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Employees' table. You can move, or remove it, as needed.
        Me.EmployeesTableAdapter.Fill(Me.NicolaouDBDataSet.Employees)
 
 
    End Sub
 
    Private Sub email_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles email.Click
        Nemail.Show()
    End Sub
    
    Private Sub callendar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles callendar.Click
        Ncalendar.Show()
    End Sub
 
    Private Sub touch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles touch.Click
        Process.Start("osk.exe")
    End Sub
 
    Private Sub infos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles infos.Click
        NicolaouInfos.Show()
    End Sub
 
    Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage1.Click
 
    End Sub
End Class

Open in new window

0
 
demoniumzAuthor Commented:
can u help me  to understanfand why is these problem exist ?
0
 
demoniumzAuthor Commented:
codecruiser you  thinks is  a problem  w with   binding source etc....
?
0
 
CodeCruiserCommented:
Sorry for delayed reply. I was playing squash in gym. I am not sure if bindingsource is the problemsource as well but generally i do not use these controls. You have declared this variable but never used it!
Dim orderdetailstable As New DataTable

What happens if you add more than two rows? Does it replace quantity in all the rows with last entered quantity?
0
 
demoniumzAuthor Commented:
if i add 3 products  the value 1 and value 3 have the same  Quantity  duplicate value.The second value remain ok

0
 
CodeCruiserCommented:
Comment out the TotalSum function. Try using a DataTable as the datasource.
0
 
demoniumzAuthor Commented:
what are u mean  expalin me ...
0
 
CodeCruiserCommented:
Remove the calls to the TotalSum first. Then use a DataTable where you are using BindingSource.
0
 
demoniumzAuthor Commented:
Then use a DataTable where you are using BindingSource. apologize but i must add a datatable?  so i must remove the bindin navigator and etc?
0
 
demoniumzAuthor Commented:
sorry i am new to   vb 2005
0
 
demoniumzAuthor Commented:
CodeCruiser is not working for me can u try to help maybe diferent code and etc?
0
 
demoniumzAuthor Commented:
if i add the textbox of the table orderdetails that belong to Orders form is working but when i save it with binding navigator i get eror that a field in table Purchase  need to edit in order to save it
0
 
demoniumzAuthor Commented:
if i add a code so t o  be unique each quantity ?
0
 
DeathraceCommented:
hey, did u try my code...what 's the problem...can u post.
0
 
demoniumzAuthor Commented:
Private Sub TotalSum()
        Dim sum As Decimal = 0
        For Each row As DataGridViewRow In PurchasedetailsDataGridView.Rows
            int cell3value = Convert.toInt32(row.Cells(3).Text);
            int cell4value = Convert.toInt32(row.Cells(4).Text);
            row.Cells(5).Value = (cell3value * cell4value);
            sum += row.Cells(5).Value
      End Sub


problem  first  the cell value3  cell value 4 and cell value 5 is not decleare the row is not declare and the ; in the end is not needed

can u help pls explain me the code you provide
0
 
demoniumzAuthor Commented:
can anyone help me please
0
 
DeathraceCommented:
hey,
i will try, first comment your sum function  TotalSum() and do not call this method and try to add value in to grid....let me know whether you can add it as you ....more over declare your DataTable globally....and insert every row on Click of your button to retain previous values.
0
 
DeathraceCommented:
please try and let me know your result.
0
 
demoniumzAuthor Commented:
ok i will try after some time thnks
0
 
demoniumzAuthor Commented:
..more over declare your DataTable globally....and insert every row on Click of your button to retain previous values what a re u mean?
0
 
demoniumzAuthor Commented:
Thnks for the help i add quantity from another table relate to the purchasedetails  table and is work 100% appreciate your help.thnks experts
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 23
  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now