提问人:dlaksmi 提问时间:9/4/2023 更新时间:9/8/2023 访问量:48
DirectCast 绑定源 DataGridView 数据绑定列表在 VB.NET
how directcast bindingsource datagridview databound List in VB.NET
问:
在我删除行datagridview后,我使用“BTNUPDATE”更新到数据库,但有一个错误,我的代码有问题。
如果在没有绑定源的情况下直接绑定到 DataGridView,则“BTNUPDATE”上没有错误,但“BTNDELETE”上出现错误
谢谢
尊敬的先生,
在我删除行datagridview后,我使用“BTNUPDATE”更新到数据库,但有一个错误,我的代码有问题。
谢谢
Dim pservice As New personService
Private _source As New BindingSource()
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
_source.DataSource = pservice.GetPersondetail()
DataGridView1.DataSource = _source
End Sub
Private Sub BTNDELETE_Click(sender As Object, e As EventArgs) Handles BTNDELETE.Click
For Each dvr As DataGridViewRow In DataGridView1.SelectedRows
If dvr IsNot Nothing Then
DataGridView1.Rows.Remove(dvr)
DataGridView1.Refresh()
End If
Next dvr
End Sub
Private Sub BTNUPDATE_Click(sender As Object, e As EventArgs) Handles BTNUPDATE.Click
Try
If DataGridView1.RowCount = 0 Then
Throw New Exception("no data")
End If
For Each item As DataGridViewRow In DataGridView1.Rows
Dim detail = DirectCast(item.DataBoundItem, persondetail)
Dim GetStocks = New persondetail With {
.Nameperson = detail.Nameperson,
.Age = detail.Age,
.Job = detail.Job,
.ID = detail.ID,
.Invono = detail.Invono}
pservice.UpdatePersondetail(detail)
Next item
MessageBox.Show("successfully")
Catch ex As Exception
MessageBox.Show(ex.Message, "myapp", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
我希望能够使用“BTNUPDATE”事件进行更新,请推荐
答:
1赞
dlaksmi
9/8/2023
#1
根据@jmcilhinney的建议
Dim pservice As New personService()
Private _source As New BindingSource()
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
_source.DataSource = pservice.GetPersondetail()
DataGridView1.DataSource = _source
End Sub
Private Sub BTNDELETE_Click(sender As Object, e As EventArgs) Handles BTNDELETE.Click
Try
If MessageBox.Show("Delete selected record?", "CONFIRM!", MessageBoxButtons.YesNoCancel) = DialogResult.Yes Then
For Each dvr As DataGridViewRow In DataGridView1.SelectedRows
Dim detail = DirectCast(dvr.DataBoundItem, persondetail)
Dim persondetail = New persondetail With {
.ID = Convert.ToInt32(detail.ID),
.Invono = detail.Invono
}
_source.remove(persondetail)
pservice.DeletePersondetail(persondetail)
'' rebind!
Me.DataGridView1.DataSource = Nothing
Me.DataGridView1.DataSource = pservice.GetPersondetail()
Next dvr
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "myapp", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub Private Sub BTNUPDATE_Click(sender As Object, e As EventArgs) Handles BTNUPDATE.Click
Try
If DataGridView1.RowCount = 0 Then
Throw New Exception("no data")
End If
For Each item As DataGridViewRow In DataGridView1.Rows
Dim detail = DirectCast(item.DataBoundItem, persondetail)
Dim persondetail = New persondetail With {
.Nameperson = detail.Nameperson,
.Age = detail.Age,
.Job = detail.Job,
.ID = detail.ID,
.Invono = detail.Invono}
pservice.UpdatePersondetail(detail)
'' rebind!
Me.DataGridView1.DataSource = Nothing
Me.DataGridView1.DataSource = pservice.GetPersondetail()
Next item
MessageBox.Show("successfully")
Catch ex As Exception
MessageBox.Show(ex.Message, "myapp", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
评论
Refresh
Remove
BindingSource
DataRow
Deleted
use ADO.NET to delete that row(s)
BTNUPDATE_Click
object reference not set to an instance of an object