2013-12-12 46 views
0

如何創建一個類或一些其他的東西,使我的項目中的所有DataGridView具有相同的格式i:e AlternativeRowColor,ForColor,BackColor和其他屬性。目前我必須去每個控件屬性進行設置,當用戶請求更改網格屬性時,它會消失,因爲我必須在所有DataGridView中進行更改。如何在VB.net中爲DataGridView提供通用樣式

回答

2
Public Class FrmArticle 

     Private Sub GridFormatting(ByVal DGV As DataGridView) 
    DGV.ForeColor = Color.Black 
    DGV.BackgroundColor = Color.AliceBlue 
    DGV.AlternatingRowsDefaultCellStyle.BackColor = Color.Brown 
    DGV.AlternatingRowsDefaultCellStyle.ForeColor = Color.DodgerBlue 
    DGV.ColumnHeadersDefaultCellStyle.ForeColor = Color.CadetBlue 
    DGV.ColumnHeadersDefaultCellStyle.BackColor = Color.DarkGoldenrod 
    DGV.EnableHeadersVisualStyles = False 
     End Sub 

     Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
       GridFormatting(DataGridView1) 
     End Sub 
    End Class 

Module GridFormat 
    Public Sub GridFormatting(ByVal DGV As DataGridView) 
    DGV.ForeColor = Color.Black 
    DGV.BackgroundColor = Color.AliceBlue 
    DGV.AlternatingRowsDefaultCellStyle.BackColor = Color.Brown 
    DGV.AlternatingRowsDefaultCellStyle.ForeColor = Color.DodgerBlue 
    DGV.ColumnHeadersDefaultCellStyle.ForeColor = Color.CadetBlue 
    DGV.ColumnHeadersDefaultCellStyle.BackColor = Color.DarkGoldenrod 
    DGV.EnableHeadersVisualStyles = False 
    End Sub 
End Module 


Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
       GridFormatting(DataGridView1) 
End Sub 
+1

我想補充一點,你可以(在模塊中爲共享方法,或者)添加爲另一個類的輔助功能。如果你想要的話,你甚至可以使它成爲擴展方法;) –

+0

在哪裏添加這個GridFormatting函數? –

+0

我們可以添加助手類或直接 – SK2185

相關問題