DirectCast 绑定源 DataGridView 数据绑定列表在 VB.NET

how directcast bindingsource datagridview databound List in VB.NET

提问人:dlaksmi 提问时间:9/4/2023 更新时间:9/8/2023 访问量:48

问:

在我删除行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”事件进行更新,请推荐

vb.net DataGridView dapper BindingSource DataBound

评论

1赞 jmcilhinney 9/4/2023
什么错误,到底在哪里?
1赞 jmcilhinney 9/4/2023
从网格中删除行是没有用的。这不会对基础数据执行任何操作。调用也是无用的,因为所做的只是在屏幕上重新绘制网格。如果希望删除数据,则需要调用相应的方法,将基础标记为,然后使用 ADO.NET 删除该行。RefreshRemoveBindingSourceDataRowDeleted
0赞 dlaksmi 9/4/2023
@jmcilhinney,如果我在没有绑定源的情况下使用它,那么我可以更新.如果我使用绑定源,则发生错误,请指导您use ADO.NET to delete that row(s)BTNUPDATE_Clickobject reference not set to an instance of an object
0赞 dlaksmi 9/4/2023
@jmcilhinney,如果我使用绑定源以便可以在 BTNUPDATE 中更新,是否有任何代码需要修复?
0赞 dlaksmi 9/8/2023
@jmcilhinney,多亏了您的指导,我终于得到了一个解决方案,对不起,如果我回复您太晚了。我的回答中有什么需要纠正的地方吗

答:

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





评论

0赞 Community 9/18/2023
您的答案可以通过额外的支持信息得到改进。请编辑以添加更多详细信息,例如引文或文档,以便其他人可以确认您的答案是正确的。您可以在帮助中心找到有关如何写出好答案的更多信息。