Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

Select a specific row from ComboBox and add amount in other Column from same row in DataGridView VB.NET

I'm using VB.NET for a small project. I have a DataGridView with some rows and columns. I want to send amounts inside a specific row & column. For this purpose I'm using a ComboBox to select the receiver, but I cant manage to select the specific row & column for the ammount.

Here is a photo of the form: https://i.stack.imgur.com/VqTX3.png

I managed to add the receivers inside the ComboBox and to add an ammount, but in all rows. This is code (it auto generates the rows for now).

    Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim rowId As Integer = Me.DataGridView1.Rows.Add()
        Dim row As DataGridViewRow = Me.DataGridView1.Rows(rowId)
        row.Cells("Column1").Value = "UNITED BANK"
        row.Cells("Column2").Value = "1000"
        row.Cells("Column3").Value = "ExampleInfo"
        Dim rowId2 As Integer = Me.DataGridView1.Rows.Add()
        Dim row2 As DataGridViewRow = Me.DataGridView1.Rows(rowId2)
        row2.Cells("Column1").Value = "FREE BANK"
        row2.Cells("Column2").Value = "2000"
        row2.Cells("Column3").Value = "ExampleInfo"
        Dim bolAdd As Boolean = False
        Dim strValue As String
        For Each myRow As DataGridViewRow In Me.DataGridView1.Rows
            bolAdd = True
            strValue = myRow.Cells("Column1").Value
            For Each myItem As String In Me.ComboBox1.Items
                If myItem = strValue Then bolAdd = False
            Next
            If bolAdd AndAlso Not (strValue Is Nothing) Then Me.ComboBox1.Items.Add(strValue)
        Next

        If Me.ComboBox1.SelectedIndex = -1 Then
            Me.ComboBox1.SelectedIndex = 0
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For Each rowX As DataGridViewRow In Me.DataGridView1.Rows
            rowX.Cells(1).Value = Val(rowX.Cells(1).Value) + Val(TextBox1.Text)
        Next
    End Sub
End Class

With this code, I'm able to add an amount, but to all rows.

I want to add an ammount only to the row I selected in the ComboBox.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your current code in the Button1_Click event is adding the amount from the text box to ALL the rows in the grid regardless of “what” value is selected in the combo box.

From what you describe, you want the amount from the text box to be added to rows that “match” the value in the combo box. The current code is never checking this, so the value from the text box is added to all the rows in the grid.

Therefore, the code needs to check to see if the value in Column1 "Receiver" “matches” the value in the combo box. If it DOES match, then add the value from the text box to the current value in Column2 "USD."

It is unclear if there may be more rows in the grid that have the same Column1 "Receiver" value and if you want to update those values also. I am assuming that there will only be ONE row in the grid that “matches” the value in the combo box. Therefore, once we find this value and add the amount, then we are done and the code will return without looping through the rest of the rows in the grid.

So is what you need to do is alter the code in the button click event to do this checking as described above.

There are a lot of checks we need to make. We need to check that the combo box has a value to compare to. Also, before we check ANY cell value, we need to make sure the cells value is not null BEFORE we try and call the cell's Value ToString method.

In addition, the cell MAY have a value, however, it may not be a number, so we have to check and make sure the value in the cell is an actual number and the same would apply to the text box.

So, walking through the code below would go something like…

  • If (Int32.TryParse(TextBox1.Text, addedValue)) … checks to see if the text box amount is a valid number.

  • If (Not String.IsNullOrEmpty(targetComboValue)) … checks to make sure the combo box value is not empty.

Then we start the loop through all the rows in the grid.

  • If (Not rowX.IsNewRow) … checks to see if the row is the “new” row which we will ignore this “new” row.

  • If (rowX.Cells("Column1").Value IsNot Nothing) … checks to make sure the “Receiver” cell is not null

  • If (rowX.Cells("Column1").Value.ToString() = targetComboValue) … checks to see if the “Receiver” cells value matches the value in the combo box.

  • If (rowX.Cells("Column2").Value IsNot Nothing) … check to make sure the “USD” cell is not null.

And Finally,…

  • If (Int32.TryParse(rowX.Cells("Column2").Value.ToString(), currentValue)) … check to see if the value in the “USD” cell is a valid number.

If all these checks succeed then add the value from the text box to the current value in the “USD” cell.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  Dim targetComboValue = ComboBox1.SelectedItem
  Dim addedValue As Int32
  Dim currentValue As Int32
  If (Int32.TryParse(TextBox1.Text, addedValue)) Then
    If (Not String.IsNullOrEmpty(targetComboValue)) Then
      For Each rowX As DataGridViewRow In Me.DataGridView1.Rows
        If (Not rowX.IsNewRow) Then
          If (rowX.Cells("Column1").Value IsNot Nothing) Then
            If (rowX.Cells("Column1").Value.ToString() = targetComboValue) Then
              If (rowX.Cells("Column2").Value IsNot Nothing) Then
                If (Int32.TryParse(rowX.Cells("Column2").Value.ToString(), currentValue)) Then
                  rowX.Cells("Column2").Value = currentValue + addedValue
                  Return
                End If
              End If
            End If
          End If
        End If
      Next
    End If
  End If
End Sub

I hope this makes sense.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...