一个新问题!!
我在datagridview中有一个复选框列,还有一个名为“partqty”的列,它显示了可用的材料数量。我希望当用户选中复选框时,应该添加并显示已检查行的数量在文本框中..
我厌倦了这样的事情..但它在文本框中显示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”类型定义运算符“+”,并键入“Boolean”
这是我试过的代码:
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
答案 0 :(得分:2)
您首先要问的是,您正尝试将苹果和橙子一起添加。
在行中:
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
有些值得注意的代码:
检查复选框是否已选中,您可以执行类似的操作:
Boolean.TryParse(row.Cells("IsChecked").Value.ToString, cb)
最后要注意的一点是,如果您尝试在DataGridView的cellclick方法中执行此代码,您将遇到问题 - 在单击单元格之后才会提交复选框状态,因此最近检查的单元格将无法获取加入你的计数。相反,您将需要处理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中看起来似乎也没有真正起作用 - 但我更像是一个c#dev,这可能是我错过了一些东西)。
相反,你想:
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
答案 1 :(得分:0)
这是有效的代码..
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
感谢您的帮助!! :)