Solved

duplicate value in datagridview

Posted on 2009-05-06
35
457 Views
Last Modified: 2013-11-26
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
Comment
Question by:demoniumz
  • 23
  • 8
  • 4
35 Comments
 
LVL 9

Accepted Solution

by:
Deathrace earned 500 total points
Comment Utility
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
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
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
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
cell3value cellvalue4 and row is not declare
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
And my suggestion?
0
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
dr and grandtotal is not decleare
0
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
what is the grandtotal?
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
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
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
grand total is not   declare i dont understand how to fix  it?
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
My Goodness. Mate put this statement somewhere

dim GrandTotal as single
0
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
lol  now i understand you sorry about this
0
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
You see now what i mean  Sage the code is not help
untitled.PNG
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
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
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
that i ask  why is changed if i know i dont ask my freind
0
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
i have the same form  for order sam way and is work
0
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
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
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
can u help me  to understanfand why is these problem exist ?
0
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
codecruiser you  thinks is  a problem  w with   binding source etc....
?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
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
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
if i add 3 products  the value 1 and value 3 have the same  Quantity  duplicate value.The second value remain ok

0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Comment out the TotalSum function. Try using a DataTable as the datasource.
0
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
what are u mean  expalin me ...
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Remove the calls to the TotalSum first. Then use a DataTable where you are using BindingSource.
0
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
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
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
sorry i am new to   vb 2005
0
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
CodeCruiser is not working for me can u try to help maybe diferent code and etc?
0
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
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
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
if i add a code so t o  be unique each quantity ?
0
 
LVL 9

Assisted Solution

by:Deathrace
Deathrace earned 500 total points
Comment Utility
hey, did u try my code...what 's the problem...can u post.
0
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
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
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
can anyone help me please
0
 
LVL 9

Expert Comment

by:Deathrace
Comment Utility
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
 
LVL 9

Expert Comment

by:Deathrace
Comment Utility
please try and let me know your result.
0
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
ok i will try after some time thnks
0
 
LVL 1

Author Comment

by:demoniumz
Comment Utility
..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
 
LVL 1

Author Closing Comment

by:demoniumz
Comment Utility
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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now