- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
- [VB.NET] CRUD Thêm xóa sửa tìm kiếm Realtime FireBase
- [C#] Hiển thị thông báo Toast Message trong lập trình Winform
- [C#] Cấu hình định dạng ngày tháng, thời gian trên Windows cho ứng dụng Winform
- [C#] Rút gọn đường dẫn link url với TinyURL API
- [C#] Hướng dẫn cách bo tròn winform with Radius
- [C#] Chia sẽ class BackGroundOverlay Show Modal cho Winform
- [C#] Hướng dẫn Flip Image Winform
- [C#] Invoke là gì? cách sử dụng phương thức Invoke()
- [C#] Hướng dẫn chia sẽ file, folder từ ứng dụng sang Zalo Chat
Hướng dẫn Thêm Xoá Sửa trực tiếp trên Gridview của Devexpress sử dụng DataAdapter cập nhật xuống database
Hôm nay, mình xin hướng dẫn, cách thêm, xoá, sửa trực tiếp trên gridview của Devexpress, đồng thời tự động cập nhật dữ liệu xuống database.
Ứng dụng mình demo, sử dụng DataAdapter để tự động cập nhật dữ liệu dựa vào dataset trên gridview.
Khi dữ liệu trên Gridview của Devexpress thay đổi, thì dataset cũng thay đổi theo.
Chúng ta, sẽ cập nhật dataset mới cập nhật dữ liệu đó xuống database.
Lưu ý: DataApdapter tự động cập nhật xuống cơ sở dữ liệu, Visual basic chỉ hỗ trợ một table, không hỗ trợ nhiều bảng table cùng 1 lúc (join database)
Giao diện chương trình:
Chương trình mình sẽ tạo, thêm button thêm và xoá vào gridview, nếu các bạn nào chưa biết cách thực hiện, có thể tham khảo bài viết:
Thêm button vào Gridview devexpress Của anh Tona: https://laptrinhvb.net/bai-viet/devexpress/Them-Button-vao-GridControl-DevExpress/66f02903edd2a656.html
Source chương trình:
Imports System.ComponentModel
Imports System.Text
Imports System.Data.SqlClient
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Grid
Partial Public Class Form1
Inherits DevExpress.XtraEditors.XtraForm
Shared Sub New()
DevExpress.UserSkins.BonusSkins.Register()
DevExpress.Skins.SkinManager.EnableFormSkins()
End Sub
Public Sub New()
InitializeComponent()
End Sub
Dim con As New SqlConnection
Public Sub Taoketnoi()
Dim str As String = "Data Source=.;Initial Catalog=sinhvien;Integrated Security=True"
con.ConnectionString = str
con.Open()
End Sub
Dim ds As DataSet
Dim da As SqlDataAdapter
Public Function LayDulieu() As DataSet
Taoketnoi()
da = New SqlDataAdapter("Select * from tbl_sinhvien", con)
Dim commandBuilder As New SqlCommandBuilder(da)
ds = New DataSet()
da.Fill(ds, "tbl_sinhvien")
Return ds
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
GridControl1.DataSource = LayDulieu.Tables("tbl_sinhvien")
GridControl1.ForceInitialize()
End Sub
Private Sub RepositoryItemButtonEdit1_Click(sender As Object, e As EventArgs) Handles RepositoryItemButtonEdit1.Click
GridView1.DeleteSelectedRows()
da.Update(ds.Tables("tbl_sinhvien"))
ds.AcceptChanges()
End Sub
Private Sub GridControl1_ProcessGridKey(sender As Object, e As KeyEventArgs) Handles GridControl1.ProcessGridKey
Dim grid = TryCast(sender, GridControl)
Dim view = TryCast(grid.FocusedView, GridView)
If e.KeyData = Keys.Delete Then
view.DeleteSelectedRows()
e.Handled = True
da.Update(ds.Tables("tbl_sinhvien"))
ds.AcceptChanges()
End If
End Sub
Private Sub RepositoryItemButtonEdit2_Click(sender As Object, e As EventArgs) Handles RepositoryItemButtonEdit2.Click
GridView1.AddNewRow()
GridView1.OptionsNavigation.AutoFocusNewRow = True
End Sub
Private Sub GridView1_RowUpdated(sender As Object, e As RowObjectEventArgs) Handles GridView1.RowUpdated
If ds.HasChanges Then
da.Update(ds.Tables("tbl_sinhvien"))
ds.AcceptChanges()
End If
End Sub
End Class
VIDEO DEMO ỨNG DỤNG
CHÚC CÁC BẠN THÀNH CÔNG!