NEWS

Change value on another combobox column in gridcontrol devexpress - Thay đổi giá trị của combobox trên gridcontrol sử dụng LookupEdit

Change value on another combobox column in gridcontrol devexpress - Thay đổi giá trị của combobox trên gridcontrol sử dụng LookupEdit
Đăng bởi: TONA Cody - Lượt xem: 21011 20:43:53, 02/01/2017DEVEXPRESS   In bài viết

Lời đầu tiên thay mặt các thành viên trên LaptrinhVB.Net, xin gửi đến các anh chị và các bạn lời chúc sức khỏe, thành đạt và ít bug trong năm mới. Cám ơn tất cả mọi người đã quan tâm đến trang trong thời gian qua. Chúc các anh chị trong nhóm nhiều may mắn, thành công, và nghiên cứu ra nhiều cái hay để chia sẻ cho mọi người. Thằng em xin phép khai bút ạ ! :)) $$$


Và hôm nay xin giớ thiệu với các bạn cách thay đổi giá trị combobox trong grid. Bài này do anh Sơn Băng nghiên cứu theo ý tưởng của anh Cái Trí Minh đưa ra. Mình thấy hay nên share ra cho mọi người.
Mình sẽ tạo ra 2 cái combobox trong cùng 1 lưới, khi thay đổi giá trị combo1 thì combo2 sẽ lọc ra những dữ liệu ít hơn theo điều kiện của combo1 đã chọn. Thực hiện thêm combobox vào lưới tương tự như thêm button, chỉ khác là mình sẽ chọn thêm LookupEdit thay vì ButtonEdit:
https://laptrinhvb.net/bai-viet/devexpress/Them-Button-vao-GridControl-DevExpress/66f02903edd2a656.html 


Devexpress-combobox-gridcontrol-1

Thực hiện tương tự cho combobox thứ 2. Xong, tiến hành đổ dữ liệu cho combobox.
1. Database:

create database DEMOGRID
go
use DEMOGRID
go
create table tinhthanh(
	matinhthanh varchar(3),
	tentinhthanh nvarchar(50)
)
go
create table quanhuyen(
	maquanhuyen varchar(3),
	matinhthanh varchar(3),
	tenquanhuyen nvarchar(50)
)
go
create table nhanvien(
	manv varchar(5),
	hoten nvarchar(50),
	cmnd varchar(11),
	matinhthanh varchar(3),
	maquanhuyen varchar(3)
)
go
insert into tinhthanh(matinhthanh,tentinhthanh) values ('1', N'Quảng Bình')
insert into tinhthanh(matinhthanh,tentinhthanh) values ('2', N'Đồng Nai')
insert into tinhthanh(matinhthanh,tentinhthanh) values ('3', N'Lạng Sơn')
insert into tinhthanh(matinhthanh,tentinhthanh) values ('4',N'Vũng Tàu')
insert into tinhthanh(matinhthanh,tentinhthanh) values ('5', N'Tp. Hồ Chí Minh')
insert into tinhthanh(matinhthanh,tentinhthanh) values ('6', N'Quảng Trị')
go
insert into quanhuyen(matinhthanh,maquanhuyen,tenquanhuyen) values ('1','LT',N'Lệ Thủy')
insert into quanhuyen(matinhthanh,maquanhuyen,tenquanhuyen) values ('1','DH',N'Đồng Hới')
insert into quanhuyen(matinhthanh,maquanhuyen,tenquanhuyen) values ('2','BH',N'Biên Hòa')
insert into quanhuyen(matinhthanh,maquanhuyen,tenquanhuyen) values ('2','DG',N'Dầu Giây')
insert into quanhuyen(matinhthanh,maquanhuyen,tenquanhuyen) values ('3','NB',N'Nõ biết')
insert into quanhuyen(matinhthanh,maquanhuyen,tenquanhuyen) values ('4','DS',N'Đông Sơn')
insert into quanhuyen(matinhthanh,maquanhuyen,tenquanhuyen) values ('4','BR',N'Bà Rịa')
insert into quanhuyen(matinhthanh,maquanhuyen,tenquanhuyen) values ('5','Q1',N'Quận Nhất')
insert into quanhuyen(matinhthanh,maquanhuyen,tenquanhuyen) values ('5','BT',N'Bình Thạnh')
insert into quanhuyen(matinhthanh,maquanhuyen,tenquanhuyen) values ('5','GV',N'Gò Vấp')
insert into quanhuyen(matinhthanh,maquanhuyen,tenquanhuyen) values ('6','GL',N'Gio Linh')
go
insert into nhanvien(manv,hoten,cmnd,matinhthanh,maquanhuyen) values ('NV01', N'NGUYỄN ĐÌNH TONA', '0123123654', '5', 'Q1')
insert into nhanvien(manv,hoten,cmnd,matinhthanh,maquanhuyen) values ('NV02', N'NGUYỄN THẢO', '9999999999', '4', 'DS')
insert into nhanvien(manv,hoten,cmnd,matinhthanh,maquanhuyen) values ('NV03', N'CÁI TRÍ MINH', '888888888', '2', 'DG')
insert into nhanvien(manv,hoten,cmnd,matinhthanh,maquanhuyen) values ('NV04', N'HOÀNG THỊ THẢO', '111111111', '3', 'NB')
go

2. Tiến hành đổ dữ liệu vào lưới chứa combobox:
Lưu ý: Khi đổ dữ liệu vào lưới, cần đổ dữ liệu vào combobox trước. Lý do thì mọi người cứ làm thử sau sẽ rõ :D.

 

Public Sub open_connect() 
        Try
            strconnect.ConnectionString = "Data Source=tona	ona;Initial Catalog=DEMOGRID;Integrated Security=True"
            strconnect.Open()
        Catch ex As Exception
        End Try
    End Sub

    Dim da As New SqlDataAdapter
    Dim ds As New DataSet

    Public Function _load_data(ByVal strLenh As String) As DataSet
        Dim ds As New DataSet
        open_connect()
        Dim cmd As New SqlDataAdapter(strLenh, strconnect)
        cmd.Fill(ds)
        strconnect.Close()
        Return ds
    End Function

    Public Sub load_cbo_quanhuyen() 'đẩy dữ lieu vào combo quận huyện
        Dim dt As New DataTable
        dt = _load_data("select maquanhuyen,matinhthanh,tenquanhuyen from quanhuyen").Tables(0)
        cbo_quanhuyen.DataSource = dt
        cbo_quanhuyen.DisplayMember = "tenquanhuyen"
        cbo_quanhuyen.ValueMember = "maquanhuyen"
    End Sub

    Public Sub load_cbo_tinhthanh() 'lấy dữ lieu vào combo tỉnh thành
        Dim ds As New DataTable
        ds = _load_data("select * from tinhthanh").Tables(0)
        cbo_tinhthanh.DataSource = ds
        cbo_tinhthanh.DisplayMember = "tentinhthanh"
        cbo_tinhthanh.ValueMember = "matinhthanh"
    End Sub

    Public Sub load_grid() ' đổ dữ lieu vào lưới
        Dim ds As New DataSet
        ds = _load_data("select * from nhanvien")
        GridControl1.DataSource = ds.Tables(0)
    End Sub

'load khi chạy form
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        load_cbo_quanhuyen()
        load_cbo_tinhthanh()

        load_grid()
    End Sub

Lúc này combobox quận huyện sẽ bung ra mọi thứ có trong bảng quanhuyen, cái mình cần là khi mình chọn tỉnh thì cbo quận huyện sẽ hiển thị chỉ những huyện thuộc tỉnh đó thôi. Mình thực hiện việc này trong event ShownEditor của AdvBandedGridView1. Lưu ý là AdvBandedGridView1 chứ không phải là GridControl1 nha. thực hiện với các câu lệnh sau:

Dim view As GridView = CType(sender, GridView)
        If view.FocusedColumn.FieldName = "maquanhuyen" AndAlso TypeOf view.ActiveEditor Is LookUpEdit Then 'Điều kiện là cột cần Filter là Mã Lớp
            Dim edit As LookUpEdit
            Dim table As DataTable
            Dim row As DataRow
            Dim clone As DataView

            edit = CType(view.ActiveEditor, LookUpEdit) 'Lấy đối tượng hiện tại để lookup Mã lớp

            table = CType(edit.Properties.DataSource, DataTable) ' Lấy datasource của đối tượng
            clone = New DataView(table) 'Chuyển sang DataView để filter
            row = view.GetDataRow(view.FocusedRowHandle) ' Lấy giá trị của dòng dữ liệu hiện tại trên advBanded
            Dim str As String = row("matinhthanh").ToString()
            clone.RowFilter = "[matinhthanh] = '" + row("matinhthanh").ToString() + "'" 'Filter makhoa theo strmakho của dòng hiện tại
            edit.Properties.DataSource = clone ' Gán datasource đã được filter cho đối tượng LookupEdit của Mã lớp
        End If

Full 5% :D (Câu này chả liên quan, fan Origin sẽ biết :3). Xong. Nhấn F5 để xem thành quả.

Change-value-combobox-gridcontrol-devexpress-thay-doi-gia-tri-combobox-tren-luoi

Chúc các bạn thành công, nhớ cho LaptrinhVB.Net biết kết quả của mọi người nhé !

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

Change value on another combobox column in gridcontrol devexpress - Thay đổi giá trị của combobox trên gridcontrol sử dụng LookupEdit
Đăng bởi: TONA Cody - Lượt xem: 21011 20:43:53, 02/01/2017DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.