Hi kana250688,
On button Update click convert the BindingSource to SortableBindingList and check if count greater than zero then update the record.
Use the following code.
VB.Net
Imports System.Data.OleDb
Imports Dapper
Public Class Form1
Private _criteriasBindingList As New SortableBindingList(Of Person)()
Private bindingSource As BindingSource = Nothing
Dim PersonService As New PersonService()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
loaddata()
End Sub
Private Sub loaddata()
_criteriasBindingList = New SortableBindingList(Of Person)(CType(PersonService.GetPERSON(), IList(Of Person)))
bindingSource = New BindingSource With {.DataSource = _criteriasBindingList}
DataGridView1.DataSource = bindingSource 'Set the data source.
End Sub
Private Sub myFilter(str1 As String, str2 As String)
If (String.IsNullOrEmpty(str1) AndAlso String.IsNullOrEmpty(str2)) Then
bindingSource.DataSource = _criteriasBindingList
ElseIf (String.IsNullOrEmpty(str1)) Then
bindingSource.DataSource = _criteriasBindingList.Where(Function(c) c.DATE.ToString("dd-MM-yyyy").Contains(str2)).ToList()
ElseIf (String.IsNullOrEmpty(str2)) Then
bindingSource.DataSource = _criteriasBindingList.Where(Function(c) c.NAMEPERSON.ToLower().Contains(str1)).ToList()
Else
bindingSource.DataSource = _criteriasBindingList.Where(Function(c) c.NAMEPERSON.ToLower().Contains(str1) AndAlso c.DATE.ToString("dd-MM-yyyy").Contains(str2)).ToList()
End If
End Sub
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TXTNAME.KeyUp
Dim str1 = TXTNAME.Text.Trim().ToLower()
Dim str2 = TXTDATE.Text.Trim().ToLower().Replace("/ /", "")
myFilter(str1, str2)
End Sub
Private Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TXTDATE.KeyUp
Dim str1 = TXTNAME.Text.Trim().ToLower()
Dim str2 = TXTDATE.Text.Trim().ToLower().Replace("/ /", "")
myFilter(str1, str2)
End Sub
Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles BtnUpdate.Click
Try
_criteriasBindingList = New SortableBindingList(Of Person)(CType(bindingSource.DataSource, IList(Of Person)))
Dim i As Integer = 0
If _criteriasBindingList.Count > 0 Then
For Each person As Person In _criteriasBindingList
Dim Update = New Person With {
.TIME = TXTTIME.Text,
.OTHERS1 = TXTOTHERS1.Text,
.OTHERS2 = TXTOTHERS2.Text,
.NAMEPERSON = person.NAMEPERSON,
.DATE = person.DATE}
PersonService.UpdatePerson(Update)
_criteriasBindingList.Item(i).TIME = TXTTIME.Text
_criteriasBindingList.Item(i).OTHERS1 = TXTOTHERS1.Text
_criteriasBindingList.Item(i).OTHERS2 = TXTOTHERS2.Text
i = i + 1
Next
bindingSource = New BindingSource With {.DataSource = _criteriasBindingList}
DataGridView1.DataSource = bindingSource
MessageBox.Show("Person In successfully updated")
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Myapp", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
End Class
Public Class Person
Public Property ID As Integer
Public Property NAMEPERSON As String
Public Property AGE As Integer
Public Property [POSITION] As String
Public Property [DATE] As DateTime
Public Property DAYS As String
Public Property TIME() As String
Public Property OTHERS1 As String
Public Property OTHERS2 As String
Public Property OTHERS3 As String
End Class
Public Class PersonService
Public Function GetOledbConnectionString() As String
Return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Person.accdb;Persist Security Info=False;"
End Function
Private ReadOnly _conn As OleDbConnection
Private _connectionString As String = GetOledbConnectionString()
Public Sub New()
_conn = New OleDbConnection(_connectionString)
End Sub
Public Function GetPERSON() As IEnumerable(Of Person)
Dim sql = "SELECT PERSON.ID AS [ID],MASTERID.NAMEPERSON AS [NAMEPERSON],PERSON.AGE AS [AGE],[PERSON.POSITION] AS [POSITION],PERSON.DATE AS [DATE],format(PERSON.DATE,'dddd') AS [DAYS],PERSON.TIME AS [TIME],PERSON.Others1 AS [OTHERS1],PERSON.Others2 AS [OTHERS2],PERSON.Others3 AS [OTHERS3] FROM PERSON INNER JOIN MASTERID ON PERSON.ID = MASTERID.ID"
Using _conn = New OleDbConnection(GetOledbConnectionString())
Return _conn.Query(Of Person)(sql).ToList()
End Using
End Function
Public Sub UpdatePerson(ByVal Obj As Person)
Try
Dim sql = $"UPDATE PERSON INNER JOIN MASTERID ON (PERSON.ID = MASTERID.ID) Set PERSON.TIME = '{Obj.TIME}',PERSON.OTHERS1 = '{Obj.OTHERS1}',PERSON.OTHERS2 = '{Obj.OTHERS2}' WHERE MASTERID.NAMEPERSON = '{Obj.NAMEPERSON}' AND PERSON.DATE = #{Obj.DATE}# ;"
Using _conn = New OleDbConnection(GetOledbConnectionString())
_conn.Execute(sql)
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "Myapp", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
End Class
Screenshot