首页 > 解决方案 > 从 ComboBox 中选择特定行并在 DataGridView VB.NET 的同一行中的其他列中添加金额

问题描述

我将 VB.NET 用于一个小项目。我有一个带有一些行和列的 DataGridView。

我想在特定行和列中发送金额。为此,我使用组合框来选择接收者,但我无法选择金额的特定行和列。

这是表格的照片:

[形式的形象

我设法在 ComboBox 内添加接收器并添加一个数量,但在所有行中。这是代码(它现在自动生成行)。

    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

使用此代码,我可以添加一个金额,但可以添加到所有行。

我只想向我在 ComboBox 中选择的行添加一个金额。

标签: vb.netdatagridviewcombobox

解决方案


您当前在Button1_Click事件中的代码是将文本框中的金额添加到网格中的所有行,而不管在组合框中选择了“什么”值。

根据您的描述,您希望将文本框中的金额添加到与组合框中的值“匹配”的行中。当前代码从不检查这一点,因此将文本框中的值添加到网格中的所有行。

因此,代码需要检查Column1“Receiver”中的值是否“匹配”组合框中的值。如果匹配,则将文本框中的值添加到Column2“USD”中的当前值。

目前尚不清楚网格中是否可能有更多行具有相同的Column1“接收者”值,以及您是否还想更新这些值。我假设网格中只有一行与组合框中的值“匹配”。因此,一旦我们找到这个值并添加数量,那么我们就完成了,代码将return不会循环遍历网格中的其余行。

因此,您需要做的是更改按钮单击事件中的代码以进行上述检查。

我们需要做很多检查。我们需要检查组合框是否有要比较的值。此外,在我们检查任何单元格值之前,我们需要确保单元格值不是null在我们尝试调用单元格的 ValueToString方法之前。

此外,单元格可以有一个值,但是,它可能不是数字,因此我们必须检查并确保单元格中的值是实际数字,并且同样适用于文本框。

所以,浏览下面的代码会像……</p>

  • If (Int32.TryParse(TextBox1.Text, addedValue))... 检查文本框金额是否为有效数字。

  • If (Not String.IsNullOrEmpty(targetComboValue))... 检查以确保组合框值不为空。

然后我们开始循环遍历网格中的所有行。

  • If (Not rowX.IsNewRow)...检查该行是否是“新”行,我们将忽略该“新”行。

  • If (rowX.Cells("Column1").Value IsNot Nothing)… 检查以确保“接收器”单元格不存在null

  • If (rowX.Cells("Column1").Value.ToString() = targetComboValue)... 检查“接收者”单元格的值是否与组合框中的值匹配。

  • If (rowX.Cells("Column2").Value IsNot Nothing)…检查以确保“美元”单元格不是null.

最后,……</p>

  • If (Int32.TryParse(rowX.Cells("Column2").Value.ToString(), currentValue))…检查“美元”单元格中的值是否为有效数字。

如果所有这些检查都成功,则将文本框中的值添加到“USD”单元格中的当前值。

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

我希望这是有道理的。


推荐阅读