Solved

duplicate value in datagridview

Posted on 2009-05-06
35
478 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
ID: 24313888
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
ID: 24314400
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
ID: 24317452
cell3value cellvalue4 and row is not declare
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24317509
And my suggestion?
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24317650
dr and grandtotal is not decleare
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24317658
what is the grandtotal?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24317695
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
ID: 24317770
grand total is not   declare i dont understand how to fix  it?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24317788
My Goodness. Mate put this statement somewhere

dim GrandTotal as single
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24317805
lol  now i understand you sorry about this
0
 
LVL 1

Author Comment

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

Expert Comment

by:CodeCruiser
ID: 24317933
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
ID: 24318061
that i ask  why is changed if i know i dont ask my freind
0
 
LVL 1

Author Comment

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

Author Comment

by:demoniumz
ID: 24318128
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
ID: 24318316
can u help me  to understanfand why is these problem exist ?
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24318454
codecruiser you  thinks is  a problem  w with   binding source etc....
?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24319215
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
ID: 24319295
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
ID: 24319412
Comment out the TotalSum function. Try using a DataTable as the datasource.
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24319422
what are u mean  expalin me ...
0
 
LVL 83

Expert Comment

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

Author Comment

by:demoniumz
ID: 24319478
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
ID: 24319485
sorry i am new to   vb 2005
0
 
LVL 1

Author Comment

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

Author Comment

by:demoniumz
ID: 24319979
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
ID: 24320375
if i add a code so t o  be unique each quantity ?
0
 
LVL 9

Assisted Solution

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

Author Comment

by:demoniumz
ID: 24334835
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
ID: 24352177
can anyone help me please
0
 
LVL 9

Expert Comment

by:Deathrace
ID: 24352528
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
ID: 24352533
please try and let me know your result.
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24353068
ok i will try after some time thnks
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24362395
..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
ID: 31578393
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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
get combo value in class 5 14
How to limit User Input 2 27
VB6 - Cannot type when doing an extract from MSHFlexgrid to Excel 6 20
Amazon S3 Images with .Net 3 15
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

820 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