Loại: UserControl
Ngôn ngữ lập trình: VB.NET
Tác giả: thuccads
Chức năng: Giữ nguyên định dạng ban đầu cho grid
-Khi thao tác với CSDL chắc chắn chúng ta không tránh khỏi làm việc với Grid, một điều khá mất công là sau khi gán datasource phải xử lý các công việc như : căn chỉnh độ rộng các cột, HeaderText, xem cột nào cần ẩn hoặc hiện....
-Nếu ta thiết lập các cột ngay trong giao diện thiết kế thì có vẻ đơn giản hơn, có điều sau khi gán datasource thì grid vẫn giữ các cột ta thêm khi thiết kế cộng với tất cả các trường đã truy vấn
.Vì vậy tôi giới thiệu cách : giữ nguyên hình dạng khi thiết kế (độ rộng các cột, HeaderText, nếu cột nào không thêm thì coi như ẩn luôn...
Đầu tiên ta tạo một datatable lưu trữ thông tin cấu hình ban đầu với các cột tương ứng là các thông tin cần lưu, các dòng để lưu giá trị. Code như sau :
Using vbnet Syntax Highlighting
Private mdtCaptions As DataTable
Public Function DefineCaption() As DataTable
Try
Me.mdtCaptions = New DataTable
Me.mdtCaptions.Columns.Add("Index", GetType(Integer))
Me.mdtCaptions.Columns.Add("Name", GetType(String))
Me.mdtCaptions.Columns.Add("HeaderText", GetType(String))
Me.mdtCaptions.Columns.Add("Visible", GetType(Boolean))
Me.mdtCaptions.Columns.Add("Width", GetType(Integer))
'Nếu thêm các thuộc tính khác thì viết tiếp ở đây.
Dim num2 As Integer = (Me.Columns.Count - 1)
Dim i As Integer = 0
Do While (i <= num2)
If (Me.Columns(i).Name <> "") Then
Dim row As DataRow = Me.mdtCaptions.NewRow
row.Item("Index") = i
row.Item("Name") = Me.Columns(i).Name
row.Item("HeaderText") = Me.Columns(i).HeaderText
row.Item("Visible") = Me.Columns(i).Visible
row.Item("Width") = Me.Columns(i).Width
'Nếu thêm các thuộc tính khác thì viết tiếp ở đây.
Me.mdtCaptions.Rows.Add(row)
End If
i += 1
Loop
'Sau khi lưu cấu hình thì xóa hết các cột hiện tại
Me.Columns.Clear()
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim exceptionCatch As Exception = exception1
Interaction.MsgBox(("T2Grid21.DefineCaption(): " & "Đã thay đổi phiên bản mới " & vbCrLf & exceptionCatch.Message), MsgBoxStyle.OkOnly, Nothing)
ProjectData.ClearProjectError()
End Try
Return mdtCaptions
End Function
Public Function DefineCaption() As DataTable
Try
Me.mdtCaptions = New DataTable
Me.mdtCaptions.Columns.Add("Index", GetType(Integer))
Me.mdtCaptions.Columns.Add("Name", GetType(String))
Me.mdtCaptions.Columns.Add("HeaderText", GetType(String))
Me.mdtCaptions.Columns.Add("Visible", GetType(Boolean))
Me.mdtCaptions.Columns.Add("Width", GetType(Integer))
'Nếu thêm các thuộc tính khác thì viết tiếp ở đây.
Dim num2 As Integer = (Me.Columns.Count - 1)
Dim i As Integer = 0
Do While (i <= num2)
If (Me.Columns(i).Name <> "") Then
Dim row As DataRow = Me.mdtCaptions.NewRow
row.Item("Index") = i
row.Item("Name") = Me.Columns(i).Name
row.Item("HeaderText") = Me.Columns(i).HeaderText
row.Item("Visible") = Me.Columns(i).Visible
row.Item("Width") = Me.Columns(i).Width
'Nếu thêm các thuộc tính khác thì viết tiếp ở đây.
Me.mdtCaptions.Rows.Add(row)
End If
i += 1
Loop
'Sau khi lưu cấu hình thì xóa hết các cột hiện tại
Me.Columns.Clear()
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim exceptionCatch As Exception = exception1
Interaction.MsgBox(("T2Grid21.DefineCaption(): " & "Đã thay đổi phiên bản mới " & vbCrLf & exceptionCatch.Message), MsgBoxStyle.OkOnly, Nothing)
ProjectData.ClearProjectError()
End Try
Return mdtCaptions
End Function
Parsed in 0.026 seconds, using GeSHi 1.0.8.4
Vậy trước khi gán datasource cho grid hãy gọi hàm này trước.
sau khi gán datasource cho grid hãy goi thủ tục lấy lại các thông tin đã lưu ở hàm DefineCaption. Code như sau :
Using vbnet Syntax Highlighting
Public Sub RestoreCaption(Optional ByVal dtCaption As DataTable = Nothing)
If (Not dtCaption Is Nothing) Then
Me.mdtCaptions = dtCaption
End If
If (((Me.mdtCaptions Is Nothing) OrElse (Me.mdtCaptions Is Nothing)) OrElse (Me.mdtCaptions.Rows.Count <= 0)) Then
Return
End If
Dim num As Integer = 0
'Ẩn toàn bộ các cột trước
While num <= Me.Columns.Count - 1
Me.Columns(num).Visible = False
num += 1
End While
Try
'Duyệt các dòng trong table đã lưu
For i As Integer = 0 To Me.mdtCaptions.Rows.Count - 1
'duyệt các cột trong grid hiện tại
For j As Integer = 0 To Me.Columns.Count - 1
If Me.Columns(j).Name = Me.mdtCaptions.Rows(i)("Name") Then
Me.Columns(j).HeaderText = Me.mdtCaptions.Rows(i)("HeaderText")
Me.Columns(j).Width = Me.mdtCaptions.Rows(i)("Width")
Me.Columns(j).Visible = True
End If
Next
Next
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim exceptionCatch As Exception = exception1
Interaction.MsgBox(("RestoreCaption: " & "Đã thay đổi phiên bản mới" & vbCrLf & exceptionCatch.Message), MsgBoxStyle.OkOnly, Nothing)
ProjectData.ClearProjectError()
End Try
End Sub
If (Not dtCaption Is Nothing) Then
Me.mdtCaptions = dtCaption
End If
If (((Me.mdtCaptions Is Nothing) OrElse (Me.mdtCaptions Is Nothing)) OrElse (Me.mdtCaptions.Rows.Count <= 0)) Then
Return
End If
Dim num As Integer = 0
'Ẩn toàn bộ các cột trước
While num <= Me.Columns.Count - 1
Me.Columns(num).Visible = False
num += 1
End While
Try
'Duyệt các dòng trong table đã lưu
For i As Integer = 0 To Me.mdtCaptions.Rows.Count - 1
'duyệt các cột trong grid hiện tại
For j As Integer = 0 To Me.Columns.Count - 1
If Me.Columns(j).Name = Me.mdtCaptions.Rows(i)("Name") Then
Me.Columns(j).HeaderText = Me.mdtCaptions.Rows(i)("HeaderText")
Me.Columns(j).Width = Me.mdtCaptions.Rows(i)("Width")
Me.Columns(j).Visible = True
End If
Next
Next
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim exceptionCatch As Exception = exception1
Interaction.MsgBox(("RestoreCaption: " & "Đã thay đổi phiên bản mới" & vbCrLf & exceptionCatch.Message), MsgBoxStyle.OkOnly, Nothing)
ProjectData.ClearProjectError()
End Try
End Sub
Parsed in 0.022 seconds, using GeSHi 1.0.8.4
Và đây là code đầy đủ cảu control này :
Using vbnet Syntax Highlighting
Imports Microsoft.VisualBasic.CompilerServices
Public Class T2Grid21
Inherits DataGridView
Private mdtCaptions As DataTable
Public Function DefineCaption() As DataTable
Try
Me.mdtCaptions = New DataTable
Me.mdtCaptions.Columns.Add("Index", GetType(Integer))
Me.mdtCaptions.Columns.Add("Name", GetType(String))
Me.mdtCaptions.Columns.Add("HeaderText", GetType(String))
Me.mdtCaptions.Columns.Add("Visible", GetType(Boolean))
Me.mdtCaptions.Columns.Add("Width", GetType(Integer))
Dim num2 As Integer = (Me.Columns.Count - 1)
Dim i As Integer = 0
Do While (i <= num2)
If (Me.Columns(i).Name <> "") Then
Dim row As DataRow = Me.mdtCaptions.NewRow
row.Item("Index") = i
row.Item("Name") = Me.Columns(i).Name
row.Item("HeaderText") = Me.Columns(i).HeaderText
row.Item("Visible") = Me.Columns(i).Visible
row.Item("Width") = Me.Columns(i).Width
Me.mdtCaptions.Rows.Add(row)
End If
i += 1
Loop
'Sau khi lưu cấu hình thì xóa hết các cột hiện tại
Me.Columns.Clear()
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim exceptionCatch As Exception = exception1
Interaction.MsgBox(("T2Grid21.DefineCaption(): " & "Đã thay đổi phiên bản mới " & vbCrLf & exceptionCatch.Message), MsgBoxStyle.OkOnly, Nothing)
ProjectData.ClearProjectError()
End Try
Return mdtCaptions
End Function
Public Sub RestoreCaption(Optional ByVal dtCaption As DataTable = Nothing)
If (Not dtCaption Is Nothing) Then
Me.mdtCaptions = dtCaption
End If
If (((Me.mdtCaptions Is Nothing) OrElse (Me.mdtCaptions Is Nothing)) OrElse (Me.mdtCaptions.Rows.Count <= 0)) Then
Return
End If
Dim num As Integer = 0
'Ẩn toàn bộ các cột trước
While num <= Me.Columns.Count - 1
Me.Columns(num).Visible = False
num += 1
End While
Try
'Duyệt các dòng trong table đã lưu
For i As Integer = 0 To Me.mdtCaptions.Rows.Count - 1
'duyệt các cột trong grid hiện tại
For j As Integer = 0 To Me.Columns.Count - 1
If Me.Columns(j).Name = Me.mdtCaptions.Rows(i)("Name") Then
Me.Columns(j).HeaderText = Me.mdtCaptions.Rows(i)("HeaderText")
Me.Columns(j).Width = Me.mdtCaptions.Rows(i)("Width")
Me.Columns(j).Visible = True
End If
Next
Next
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim exceptionCatch As Exception = exception1
Interaction.MsgBox(("RestoreCaption: " & "Đã thay đổi phiên bản mới" & vbCrLf & exceptionCatch.Message), MsgBoxStyle.OkOnly, Nothing)
ProjectData.ClearProjectError()
End Try
End Sub
End Class
Public Class T2Grid21
Inherits DataGridView
Private mdtCaptions As DataTable
Public Function DefineCaption() As DataTable
Try
Me.mdtCaptions = New DataTable
Me.mdtCaptions.Columns.Add("Index", GetType(Integer))
Me.mdtCaptions.Columns.Add("Name", GetType(String))
Me.mdtCaptions.Columns.Add("HeaderText", GetType(String))
Me.mdtCaptions.Columns.Add("Visible", GetType(Boolean))
Me.mdtCaptions.Columns.Add("Width", GetType(Integer))
Dim num2 As Integer = (Me.Columns.Count - 1)
Dim i As Integer = 0
Do While (i <= num2)
If (Me.Columns(i).Name <> "") Then
Dim row As DataRow = Me.mdtCaptions.NewRow
row.Item("Index") = i
row.Item("Name") = Me.Columns(i).Name
row.Item("HeaderText") = Me.Columns(i).HeaderText
row.Item("Visible") = Me.Columns(i).Visible
row.Item("Width") = Me.Columns(i).Width
Me.mdtCaptions.Rows.Add(row)
End If
i += 1
Loop
'Sau khi lưu cấu hình thì xóa hết các cột hiện tại
Me.Columns.Clear()
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim exceptionCatch As Exception = exception1
Interaction.MsgBox(("T2Grid21.DefineCaption(): " & "Đã thay đổi phiên bản mới " & vbCrLf & exceptionCatch.Message), MsgBoxStyle.OkOnly, Nothing)
ProjectData.ClearProjectError()
End Try
Return mdtCaptions
End Function
Public Sub RestoreCaption(Optional ByVal dtCaption As DataTable = Nothing)
If (Not dtCaption Is Nothing) Then
Me.mdtCaptions = dtCaption
End If
If (((Me.mdtCaptions Is Nothing) OrElse (Me.mdtCaptions Is Nothing)) OrElse (Me.mdtCaptions.Rows.Count <= 0)) Then
Return
End If
Dim num As Integer = 0
'Ẩn toàn bộ các cột trước
While num <= Me.Columns.Count - 1
Me.Columns(num).Visible = False
num += 1
End While
Try
'Duyệt các dòng trong table đã lưu
For i As Integer = 0 To Me.mdtCaptions.Rows.Count - 1
'duyệt các cột trong grid hiện tại
For j As Integer = 0 To Me.Columns.Count - 1
If Me.Columns(j).Name = Me.mdtCaptions.Rows(i)("Name") Then
Me.Columns(j).HeaderText = Me.mdtCaptions.Rows(i)("HeaderText")
Me.Columns(j).Width = Me.mdtCaptions.Rows(i)("Width")
Me.Columns(j).Visible = True
End If
Next
Next
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim exceptionCatch As Exception = exception1
Interaction.MsgBox(("RestoreCaption: " & "Đã thay đổi phiên bản mới" & vbCrLf & exceptionCatch.Message), MsgBoxStyle.OkOnly, Nothing)
ProjectData.ClearProjectError()
End Try
End Sub
End Class
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
Chú ý là sẽ có thể dùng ngay được nhưng có một lỗi nhỏ, các bạn tự tìm và sửa cho hoàn chỉnh nhé
. Không khó đâu.


