0

Why doesn't qty column increment when adding in textbox to datagridview in vb.net.

so the qty column can only max the value to 2 is there something wrong with my code should continue to grow if the product code is the same as the product column in the DataGridView.

Thanks

 Private Sub BindItemDetail()
            If _myTable.Rows.Count = 0 Then
                Dim field() As String = {"No", "Codeproduct", "Barcode", "Qty"}
                _myTable = DataControl.CreateDataTableDynamic(field)
            End If
            grid.DataSource = _myTable
        End Sub
 Private Sub FillDataTable(iRow As Integer, ByVal Codeproduct As String, ByVal Barcode As String, ByVal Qty As Integer, ByVal Coledit As String, ByVal ColDel As String)

            Dim row As DataRow = _myTable.NewRow()
            row("No") = iRow
            row("Codeproduct") = Codeproduct
            row("Barcode") = Barcode
            row("Qty") = Qty
            _myTable.Rows.Add(row)
End Sub
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim iRow As Integer
        If Grid.RowCount - 1 = 0 Then
            iRow = 1
        Else
            iRow = Convert.ToInt32(Grid.Rows(Grid.RowCount - 2).Cells(0).Value.ToString()) + 1
        End If
        Dim Qty As Integer = 1
        Dim Found As Boolean = False
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
            For j = 0 To Me.Grid.Rows.Count - 1
                If Convert.ToString(Me.Grid.Rows(j).Cells(1).Value) = TextBox1.Text.ToString() Then
                    Found = True

'The problem in my code is in below the code line
                    Grid.Rows(j).Cells(3).Value = Qty + 1
                    TextBox1.Clear()
                    TextBox2.Clear()
                    Exit For
                End If
            Next
            If Not Found Then
                FillDataTable(iRow, TextBox1.Text, TextBox2.Text, Qty)
                Grid.DataSource = _myTable
                TextBox1.Clear()
                TextBox2.Clear()
            End If
        End If
    End Sub

result column qty in datagridview

Desired result

No CodeProduct Barcode Qty
1 1000 1000 3
2 1002 1002 1
roy
  • 693
  • 2
  • 11
  • You need to actually debug your code. Set a breakpoint and step through the code by line, to see what it actually does. If you still can't work out what the problem is, you need to explain to us exactly what the code is supposed to do and where and how what it actually does differs from that. You're not as user so don't diagnose issues like users would, simply by looking at the UI. You're a developer so do development, which includes debugging. If you don't know how to debug, stop what you're doing and learn that first. – jmcilhinney Jul 06 '23 at 04:43
  • @jmcilhinney , Thank you for your reply . The problem in my code is in the code line `Grid.Rows(j).Cells(3).Value = Qty + 1` – roy Jul 06 '23 at 04:55
  • And what is the problem? Have you actually thought about what that code does and whether that is what it's supposed to do? You set `Qty` to 1 and then you add 1 to that and set the `Value` of the cell to the result. Does that make sense? If what you actually want to do is increment the value in the cell, shouldn't you be starting with the value in the cell? This is what happens when you write code without a clear understand of the logic you're trying to implement. The code you have doesn't do what you want because you don't know what you want to do. You know the result but not the steps. – jmcilhinney Jul 06 '23 at 05:32
  • @jmcilhinney , `And what is the problem?` Thank you for your reply. should continue to increase qty column if codeproduct is the same ,I know I applied the wrong step or method please guide me – roy Jul 06 '23 at 06:54
  • I already did guide you but you ignored it. I told you to consider the logic that needs to be implemented, which you haven't done. I told you to start with the value of the cell, which you haven't done. If you expect to increment something, you have to get the old value out, increase it and then put the new value in. Where are you doing that first step> Nowhere, despite the fact that I already told you to do it. This is what I mean about considering logic. If you had to increment something on paper, you'd never start wit 1 every time. You know what to do but you ignore that when writing code. – jmcilhinney Jul 06 '23 at 07:39

2 Answers2

2

The problem with the code is you are setting the cell value to 2, not incrementing it. You set this:

        Dim Qty As Integer = 1

and then do this:

Grid.Rows(j).Cells(3).Value = Qty + 1

So if you actually want to increment the cell value by 1, then you would instead use this:

Dim newValue As Integer
If Integer.TryParse(Grid.Rows(j).Cells(3).Value, newValue) Then
    newValue += 1
Else
    newValue = 1
End If
Grid.Rows(j).Cells(3).Value = newValue.ToString()

and not define the Qty variable at all.

While at it, you probably want to check for the Enter key first, since it seems you don't want to do anything else in this method for other keys. This would be the revised code:

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
        Dim iRow As Integer
        If Grid.RowCount - 1 = 0 Then
            iRow = 1
        Else
            iRow = Convert.ToInt32(Grid.Rows(Grid.RowCount - 2).Cells(0).Value.ToString()) + 1
        End If
        Dim Found As Boolean = False
        For j = 0 To Me.Grid.Rows.Count - 1
            If Convert.ToString(Me.Grid.Rows(j).Cells(1).Value) = TextBox1.Text.ToString() Then
                Found = True
                Dim newValue As Integer
                If Integer.TryParse(Grid.Rows(j).Cells(3).Value, newValue) Then
                    newValue += 1
                Else
                    newValue = 1
                End If
                Grid.Rows(j).Cells(3).Value = newValue.ToString()
                TextBox1.Clear()
                TextBox2.Clear()
                Exit For
            End If
        Next
        If Not Found Then
            FillDataTable(iRow, TextBox1.Text, TextBox2.Text, 1)
            Grid.DataSource = _myTable
            TextBox1.Clear()
            TextBox2.Clear()
        End If
    End If
End Sub

I have to assume that if it is not found then the quantity should now be 1, but I don't have the specifications for your project, nor access to all of the objects, such as TextBox1, TextBox1, Grid, nor the FillDataTable method. If one presses Enter several times, this method would keep incrementing the value by 1.

EDIT: Per Dr. Null's suggestion, changed to ensure data type compatibility.

Michael Foster
  • 420
  • 6
  • 12
  • Use to `Option Strict On` please. You shouldn't have code like `Grid.Rows(j).Cells(3).Value += 1`. The `.Value` is of type object here and we don't increment objects but their unboxed numeric values (value type) if any. Try parse the value to get the integer value, `++` and reassign it to the `.Value` property. The same applies to the [other](https://stackoverflow.com/a/76626717/14171304) answer. – dr.null Jul 08 '23 at 12:32
  • @dr.null , Do you have a recommendation answer because I'm sure your answer must be perfect – roy Jul 11 '23 at 12:09
  • @mbmt, actually, I think he's right in that it is sloppy code. We only have the assumption that the cell value can be interpreted as an integer. I haven't run the code, because I am missing some, and only took your original code, but I'll edit in something that takes that into account. But as a note, `Option Strict` can be set in Visual Studio, so need not necessarily be in the actual code file. – Michael Foster Jul 11 '23 at 12:19
  • @MichaelFoster , OK, I updated the method `filldatatable` – roy Jul 11 '23 at 12:31
  • @MichaelFoster , I try your code for column qty starting from value 0 – roy Jul 11 '23 at 12:36
  • Then the quantity column is not an integer. What is the value in the cell? I don't have specifications, so guessed that if it isn't an integer then you must want 0. Should it instead be 1? – Michael Foster Jul 11 '23 at 12:38
  • @MichaelFoster , Should be one – roy Jul 11 '23 at 12:39
  • @MichaelFoster , I tried to fix your code, it still starts from 0 – roy Jul 11 '23 at 12:43
  • I edited it once more, changing the 0 to 1. – Michael Foster Jul 11 '23 at 12:45
  • @MichaelFoster , I have tried the last fix, your code still starts from 0 – roy Jul 11 '23 at 12:48
  • I cannot tell what the data in your grid is, and cannot step through your code to see what variable values are what when. You can change it from 1 to, say, 7, to ensure the code is updating as expected. Maybe you must `Trim` the cell value before evaluating it? – Michael Foster Jul 11 '23 at 12:52
  • @MichaelFoster , `Dim newValue As Integer = 1` if i make code like this then start start from 1 – roy Jul 11 '23 at 13:01
  • Good day gentlemen. @mbmt I've [covered](https://stackoverflow.com/a/76612495/14171304) the _options_ part already. Now, you have here the grid bound to a DataTable. Then deal with the `DT` to manipulate the data not the grid. The grid will reflect that. Cast the `Row.DataBoundItem` to `DataRowView` and modify the values as you need including that `Qty` field. Btw, you don't need the `No` and `Qty` fields in your database. Those can be _expression_ columns you can add right after filling the datatable from the database. That's another Q&A. – dr.null Jul 11 '23 at 13:56
  • @dr.null , `Btw, you don't need the No and Qty fields in your database. Those can be expression columns you can add right after filling the datatable from the database.` how can I do this please guide me the way you do recommendation – roy Jul 11 '23 at 14:29
  • @mbmt As you noticed, we can't add answers here anymore. Accept an answer here if one answers your question and ask a new question about _How to add an expression column that counts the quantity?_ or so. I recommend reading [DataColumn.Expression](https://learn.microsoft.com/en-us/dotnet/api/system.data.datacolumn.expression?view=net-7.0) beforehand. Good luck. – dr.null Jul 11 '23 at 15:23
  • @MichaelFoster , `Dim newValue As Integer = 1` Can you edit your answer like this code so I can accept your answer – roy Jul 12 '23 at 02:57
  • @dr.null , `How to add an expression column that counts the quantity?` Well I'll make a new question post – roy Jul 12 '23 at 02:58
  • @dr.null , I've made a new post I hope you can help me . https://stackoverflow.com/questions/76666968/how-to-add-an-expression-column-datatable-that-create-sequence-numbers-and-count – roy Jul 12 '23 at 03:24
0

thanks for the guide @jmcilhinney

Grid.Rows(j).Cells(3).Value += Qty

roy
  • 693
  • 2
  • 11
  • As @jmcihinney said, this doesn't increase the value of the cell. It works by coincidence. – Michael Foster Jul 06 '23 at 16:12
  • @MichaelFoster , previous code `Grid.Rows(j).Cells(3).Value = Qty + 1' and code now `Grid.Rows(j).Cells(3).Value += Qty' my answer that I posted after the comment from @jimjmcilhinney . Have you tried my code, my code I've used successfully. If you state by chance you should also state an answer. Before you comment I hope you can try the code – roy Jul 07 '23 at 02:13
  • @jmcihinney's comments were trying to point you to how analyze the code to determine what it is doing wrong. I haven't tried your answer, but I can see it increments the cell value by the amount of `Qty`. You're setting `Qty` to 1. It is an elaborate way of incrementing by 1. `Grid.Rows(j).Cells(3).Value += 1` would do the same thing, but you would not need the `Qty` variable at all. In other words, I wouldn't want to maintain the code in your answer. – Michael Foster Jul 07 '23 at 11:54
  • @MichaelFoster , if you say it's complicated and doesn't match my answer then you have to recommend and provide the best answer in your opinion – roy Jul 07 '23 at 13:34
  • @MichaelFoster , `Grid.Rows(j).Cells(3).Value += 1` is same with `Grid.Rows(j).Cells(3).Value += Qty` – roy Jul 07 '23 at 13:42
  • 1
    It certainly is, which is why you shouldn't use `Qty` for this. It is misleading, since the variable name implies a quantity. Even if you wanted to increment by a different amount, you should make a variable like `Increment` and assign it a value of 1, or whatever amount you want. Imagine looking over this code in a couple years. You read the `Qty` variable, and think it has the quantity in the cell, which it does not. – Michael Foster Jul 11 '23 at 12:16