i’m getting confuse about dataset and dataview, which i either use dataset to update data or dataview to filter data, but cannot have both together at the same time.
Private Sub searchStaff_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ds = New DataSet
dv = New DataView
Dim ad As New SqlDataAdapter("SELECT * FROM staff", connection)
ad.Fill(ds, "staff")
dv.Table = ds.Tables("staff")
Me.DataGridView1.DataSource = dv
End Sub
Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
Try
dv.RowFilter = "StaffName like '" & txtSearch.Text & "*'"
Catch ex As Exception
End Try
End Sub
this is the code of filtering data on dataview,which work fine for me.
but later on, i have to implement another function of update data on datagridview, what i found on internet require me to use dataset as datasource rather than dataview.
Private Sub searchStaff_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
connection = New SqlConnection(connectionString)
ds = New DataSet
cmd = New SqlCommand("SELECT * FROM staff", connection)
adp=New SqlDataAdapter(cmd)
adp.Fill(ds, "staff")
Me.DataGridView1.DataSource = ds
Me.DataGridView1.DataMember = "staff"
End Sub
Private Sub btnUpdate_ClickEvent(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.ClickEvent
Dim cmdbuilder As New SqlCommandBuilder(adp)
Dim i As Integer
Try
i = adp.Update(ds, "staff")
MsgBox("Record updated= " & i)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
so i hope some 1 can show me, how to i can have both functions”update,filter” at the same time by using what datasource?
You must use DataSet (or DataTable) with SqlDataAdapter for make an Update operation; DataView is used for filter data but not to commit changes to database. Here an example for your searchStaff_Load method:
The txtSearch_TextChanged and btnUpdate_ClickEvent are already correct. The first filter using DataView, the second update database using DataSet.