none
how do i update datagridview column for quantity if i added a quantity from textbox with the same column item name vb.net RRS feed

  • Question

  • HI !

    I having a problem with my code. I made a code that the data from datagridview1 that i send to dtagridview2 is not duplicated , but the problem is the column for quantity doesn't updating but the total is keep adding what will i do.

    this is my code that will prevent the data from datagridview1 to send the same data to datagridview2:

      For Each row In CashRegister.BunifuCustomDataGrid2.Rows()
                        If TextBox1.Text = row.Cells("itemcol").Value Then
                            test = True
                            Exit For
                        End If
                    Next

    Sunday, September 2, 2018 11:36 AM

Answers

  • Hi

    If my understanding of your question is correct, then here is some very basic code that may help.

    It uses the Mouse DoubleClick on the first DGV row header cell to open a Dialog for user to enter new total for the item and updates the quantity on DataGridView2.

    ' Form1 with DataGridView1 and
    ' DataGridView2
    ' Using Double Click on DataGridView1
    ' Row Header cells
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With DataGridView1
          .Rows.Add("One")
          .Rows.Add("Two")
          .Rows.Add("Three")
          .Rows.Add("Four")
        End With
        With DataGridView2
          .Rows.Add("Three", 11)
          .Rows.Add("One", 1)
          .Rows.Add("Four", 0)
        End With
      End Sub
    
      Private Sub DataGridView1_RowHeaderMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.RowHeaderMouseDoubleClick
        Dim qty As Integer = Nothing
        Dim ind As Integer = -1
        Dim found As Boolean = False
        Dim comp As String = DataGridView1(0, e.RowIndex).Value.ToString
        For Each r As DataGridViewRow In DataGridView2.Rows
          ind += 1
          Dim comp2 As String = r.Cells(0).Value.ToString
          If comp2 = comp Then
            qty = CInt(r.Cells(1).Value.ToString)
            Dim inp As String = InputBox("Enter quantity", "Quantity", qty.ToString)
            DataGridView2(1, ind).Value = inp
            found = True
            Exit For
          End If
        Next
        If Not found Then
          DataGridView2.Rows.Add(comp, InputBox("Enter quantity", "Quantity", "0"))
        End If
      End Sub
    End Class
    



    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, September 4, 2018 1:23 PM Forgot to deal with new row
    • Marked as answer by euchan09 Thursday, September 6, 2018 3:46 AM
    Tuesday, September 4, 2018 1:12 PM

All replies

  • It is not clear what you are describing since we dont have the data or the code where test is used.

    You should use Option Strict ON to help point out the errors in your code.

    Option Strict On
    
    Public Class Form6
        Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim test As Boolean
    
            For Each row As DataGridViewRow In DataGridView1.Rows()
                If TextBox1.Text Is row.Cells("itemcol").Value Then
                    test = True
                    Exit For
                End If
            Next
    
        End Sub
    End Class

    Sunday, September 2, 2018 1:59 PM
  • Hi,

    Do you want this effect, do not allow duplicate columns to be passed to the DataGridview?

    code:

    Imports System.Data.SqlClient
    Public Class Form1
        Dim constr As String = "Data Source = (localdb)\MSSQLLocalDB; Integrated Security = True ;AttachDbFileName= C:\Users\alexl2\Desktop\DataBase\Alex\alex.mdf"
        Dim conn As SqlConnection
        Dim sda As SqlDataAdapter
        Dim dt1 As New DataTable
        Dim dt2 As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Using conn = New SqlConnection(constr)
                conn.Open()
                sda = New SqlDataAdapter("Select * From Student", conn)
                sda.Fill(dt1)
                DataGridView1.DataSource = dt1
            End Using
            For i = 0 To DataGridView1.Rows.Count - 1
                dt2.Rows.Add()
            Next
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim list As List(Of String) = New List(Of String)
            For i = 0 To DataGridView2.Rows.Count - 1
                list.Add(DataGridView2.Columns(0).HeaderText)
            Next
            If list.Contains(TextBox1.Text) Then
                MessageBox.Show("error,repeat")
            Else
                dt2.Columns.Add(TextBox1.Text)
                For i = 0 To DataGridView1.Rows.Count - 1
                    dt2.Rows(i)(TextBox1.Text) = DataGridView1.Rows(i).Cells(TextBox1.Text).Value
                Next
                DataGridView2.DataSource = dt2
            End If
    
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, September 3, 2018 2:55 AM
  • hello?

    My program is when you click a row from datagridview1 theres a form that will ask how many quantity to put, after putting the quantity the data will send to datadridview2 what i want to happen is when i click the same row in datagridview1 that is already send to datagridview2 will not duplicated and the column for quantity will just updated.

    Thank you!

    Tuesday, September 4, 2018 6:09 AM
  • Hi,

    I can't understand what you mean, how the data is from DataGridview1 to DataGridview2?

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 4, 2018 9:41 AM
  • Hi

    If my understanding of your question is correct, then here is some very basic code that may help.

    It uses the Mouse DoubleClick on the first DGV row header cell to open a Dialog for user to enter new total for the item and updates the quantity on DataGridView2.

    ' Form1 with DataGridView1 and
    ' DataGridView2
    ' Using Double Click on DataGridView1
    ' Row Header cells
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With DataGridView1
          .Rows.Add("One")
          .Rows.Add("Two")
          .Rows.Add("Three")
          .Rows.Add("Four")
        End With
        With DataGridView2
          .Rows.Add("Three", 11)
          .Rows.Add("One", 1)
          .Rows.Add("Four", 0)
        End With
      End Sub
    
      Private Sub DataGridView1_RowHeaderMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.RowHeaderMouseDoubleClick
        Dim qty As Integer = Nothing
        Dim ind As Integer = -1
        Dim found As Boolean = False
        Dim comp As String = DataGridView1(0, e.RowIndex).Value.ToString
        For Each r As DataGridViewRow In DataGridView2.Rows
          ind += 1
          Dim comp2 As String = r.Cells(0).Value.ToString
          If comp2 = comp Then
            qty = CInt(r.Cells(1).Value.ToString)
            Dim inp As String = InputBox("Enter quantity", "Quantity", qty.ToString)
            DataGridView2(1, ind).Value = inp
            found = True
            Exit For
          End If
        Next
        If Not found Then
          DataGridView2.Rows.Add(comp, InputBox("Enter quantity", "Quantity", "0"))
        End If
      End Sub
    End Class
    



    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, September 4, 2018 1:23 PM Forgot to deal with new row
    • Marked as answer by euchan09 Thursday, September 6, 2018 3:46 AM
    Tuesday, September 4, 2018 1:12 PM
  • thank you
    Thursday, September 6, 2018 3:45 AM