how directcast bindingsource datagridview databound List in VB.NET

62 views Asked by At

after I delete in row datagridview then I update to database with "BTNUPDATE" but there is an error is there something wrong with my code.

if binding directly to datagridview without binding source then there is no error on "BTNUPDATE" but error on "BTNDELETE"

Thanks

Dear sir,

after I delete in row datagridview then I update to database with "BTNUPDATE" but there is an error is there something wrong with my code.

Thanks

 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

I want to be able to update with the "BTNUPDATE" event please recommend

1

There are 1 answers

1
AudioBubble On

as recommended by @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