从datagridview复选框到文本框

问题描述:

新问题!从datagridview复选框到文本框

我在datagridview中有一个复选框列,还有一个名为“partqty”的列,它显示了可用的matera资源数量。我希望当用户选中复选框时,应该添加已检查行的数量,在文本框中显示..

我厌倦了这样的事情。但它显示为0的文本框(零)。请帮助..

Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click 

     'For Each _rw As DataGridViewRow In dataGridView1.Rows 
     ' If _rw.Cells(0).Value = True Then 

     Dim totalSum As Integer 

     For i As Integer = 0 To HemDatabase1DataSet5.Tables(0).Rows.Count - 1 
      totalSum += HemDatabase1DataSet5.Tables(0).Rows(i).Item("partqty") 
     Next 

     textBox5.Text = totalSum.ToString() 
     ' End If 
     'Next 
    End Sub 

嘿!这是我可以想到的尝试!然而,在编译时没有错误,但在运行时出现错误,它表示: 运算符'+'未针对类型'DBNull'进行定义并且类型为'布尔'

这是我试过的代码:

For Each _rw As DataGridViewRow In dataGridView1.Rows 
      If _rw.Cells(0).Value = True Then 

       Dim totalSum As Integer 

       For i As Integer = 0 To dataGridView1.Rows.Count - 1 
        totalSum += dataGridView1.Rows(i).Cells(5).Value 
       Next 

       textBox5.Text = totalSum.ToString() 
      End If 
     Next 

它详细介绍了此行错误:

totalSum += dataGridView1.Rows(i).Cells(5).Value 

你的第一个问题是,你正试图添加苹果和橘子一起。

在行:

totalSum += dataGridView1.Rows(i).Cells(5).Value 

totalSum是一个整数,而一个DataGridView细胞的Value属性是object类型。

这与您在尝试使用DataTable时看到的类似问题除了您的Item属性为您提供DBNull而不是对象后退之外。这里是你想要的代码(我主要是一个C#开发,所以VB.Net可能缺乏优雅,但它的工作原理)。

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
    Dim sum As Integer 
    sum = 0 

    For Each row As DataGridViewRow In DataGridView1.Rows 
     Dim current As Integer 

     If Integer.TryParse(row.Cells("OrderQty").Value.ToString, current) Then 
      sum += current 
     End If 
    Next 
    TextBox1.Text = sum.ToString 
End Sub 

有些事情值得一提的有关代码:

  • 我使用For Each,而不是一个。这两个表现基本相同,但我发现foreach更具可读性。
  • 在使用它之前,我在单元格值上使用了Integer.TryParse。这比仅仅直接施加单元格值更安全。
  • 而不是使用列索引这是有点脆弱我使用列名称。

随着的复选框是否被选中,你可以做同样的事情你的检查:

Boolean.TryParse(row.Cells("IsChecked").Value.ToString, cb) 

最后一点要注意的是,如果你试图在cellclick执行这段代码DataGridView的方法会遇到问题 - 复选框状态只有在单击单元格之后才会提交,因此最近检查的单元格不会添加到您的计数中。相反,您需要处理CellDirtyStateChanged并提交编辑,然后在CellValueChanged处理程序中执行求和。

为了说明试试这个代码:

Private Sub dgv_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick 
    Dim sum As Integer 
    sum = 0 

    Dim cb As Boolean 
    cb = False 

    If DataGridView1.Columns(e.ColumnIndex).Name <> "IsChecked" Then 
     Return 
    End If 


    For Each row As DataGridViewRow In DataGridView1.Rows 
     If Boolean.TryParse(row.Cells("IsChecked").Value.ToString, cb) Then 
      If cb Then 
       Dim current As Integer 

       If Integer.TryParse(row.Cells("OrderQty").Value.ToString, current) Then 
        sum += current 
       End If 
      End If 
     End If 
    Next 
    TextBox1.Text = sum.ToString 
End Sub 

总和将始终落后点击后面的(实际上在VB.Net它似乎只是也不真正的工作 - 但我更AC#开发,使可能是我错过了一些东西)。

相反,你想:

Sub dataGridView1_CurrentCellDirtyStateChanged(_ 
    ByVal sender As Object, ByVal e As EventArgs) _ 
    Handles DataGridView1.CurrentCellDirtyStateChanged 

    If DataGridView1.IsCurrentCellDirty Then 
     DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit) 
    End If 
End Sub 

' If a check box cell is clicked, this event handler disables 
' or enables the button in the same row as the clicked cell. 
Public Sub dataGridView1_CellValueChanged(ByVal sender As Object, _ 
    ByVal e As DataGridViewCellEventArgs) _ 
    Handles DataGridView1.CellValueChanged 

    If DataGridView1.Columns(e.ColumnIndex).Name = "IsChecked" Then 
     Dim sum As Integer 
     sum = 0 

     Dim cb As Boolean 



     For Each row As DataGridViewRow In DataGridView1.Rows 
      If Boolean.TryParse(row.Cells("IsChecked").Value.ToString, cb) Then 
       If cb Then 
        Dim current As Integer 

        If Integer.TryParse(row.Cells("OrderQty").Value.ToString, current) Then 
         sum += current 
        End If 
       End If 
      End If 
     Next 
     TextBox1.Text = sum.ToString 
    End If 
End Sub 
+0

+1我认为你钉了它。 – ChrisBD

+0

嗯,我把这个代码在按钮单击事件..我也第一次尝试vb.net,所以谢谢你的所有解释..它有助于更​​新自我..我vl试试这个,很快恢复..再次Thanx ! :) – Ruchi22

+0

嘿..这似乎并没有为我工作..我使用此代码,并尝试了一些修改将它放在按钮单击事件!你能帮我修改我的代码吗?我发布的代码的第二部分是我正在使用的!谢谢你:) :) – Ruchi22

这是工作的代码..

Dim iSselected As Boolean 
     Dim CurrentCellValue As String 
     Dim CumulativeSummer As Integer 
     Dim i As Integer 

     With Me.dataGridView1 
      For i = 0 To .RowCount - 1 
       iSselected = .Rows(i).Cells(0).Value 
       CurrentCellValue = .Rows(i).Cells(5).Value 
       If CurrentCellValue = "" Then CurrentCellValue = 0 

       If iSselected = True Then 
        CumulativeSummer += Convert.ToInt32(CurrentCellValue) 
       End If 
      Next 
     End With 

     Me.textBox5.Text = CumulativeSummer 

感谢ü您的帮助! :)