Public Class Form1
Inherits Form
Private WithEvents dataGrid1 As DataGrid
Private components As Container
Public Sub New()
MyBase.New()
'
' Required for Windows Form Designer support
'
InitializeComponent()
'
' TODO: Add any constructor code after InitializeComponent call
'
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If (Not (components) Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private Sub InitializeComponent()
Me.dataGrid1 = New System.Windows.Forms.DataGrid()
CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
' dataGrid1
'
Me.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right)
Me.dataGrid1.DataMember = ""
Me.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.dataGrid1.Location = New System.Drawing.Point(40, 24)
Me.dataGrid1.Name = "dataGrid1"
Me.dataGrid1.Size = New System.Drawing.Size(576, 208)
Me.dataGrid1.TabIndex = 0
'
' Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(648, 253)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.dataGrid1})
Me.Name = "Form1"
Me.Text = "Form1"
AddHandler Me.Load, New System.EventHandler(AddressOf Form1_Load)
CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
<STAThread()> _
Public Shared Sub Main()
Application.Run(New Form1())
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Me.dataGrid1.DataSource = SomeDataTable()
AddCellFormattingColumnStyles(Me.dataGrid1, New FormatCellEventHandler(AddressOf FormatGridCells))
'add an event handler to select whole rows only
'uncomment this if you want this behavior
'this.dataGrid1.CurrentCellChanged += new EventHandler(SelectWholeRow);
End Sub
Private Sub FormatGridCells(ByVal sender As Object, ByVal e As DataGridFormatCellEventArgs)
'color row 1 red
'color column 4 blue
If (e.Row = 1) Then
e.BackBrush = Brushes.Red
End If
'set font of some cells to bold
If (e.Column = 4) Then
e.BackBrush = Brushes.Blue
End If
'set textcolor of some cells to blue
If (((e.Row + e.Column) Mod 5) = 0) Then
e.TextFont = New Font(e.TextFont.Name, e.TextFont.Size, FontStyle.Bold)
End If
'set font of some cells to bold, underline, italic with white text on green background
If (((e.Row + e.Column) Mod 8) = 0) Then
e.ForeBrush = Brushes.DodgerBlue
End If
If (((e.Row + e.Column) Mod 9) = 0) Then
e.TextFont = New Font(e.TextFont.Name, e.TextFont.Size, ((FontStyle.Bold Or FontStyle.Italic) _
Or FontStyle.Underline))
e.ForeBrush = Brushes.White
e.BackBrush = Brushes.Green
End If
End Sub
Private Sub SelectWholeRow(ByVal sender As Object, ByVal e As EventArgs)
If (Me.dataGrid1.CurrentRowIndex > -(1)) Then
Me.dataGrid1.Select(Me.dataGrid1.CurrentRowIndex)
End If
End Sub
Private Function SomeDataTable() As DataTable
Dim dt As DataTable
dt = New DataTable("MyTable")
'add some columns
Dim nCols As Integer
nCols = 10
Dim j As Integer
j = 0
Do While (j < nCols)
dt.Columns.Add(New DataColumn(System.String.Format("col{0}", j), GetType(System.String)))
j = (j + 1)
Loop
'add some rows
Dim nRows As Integer
nRows = 40
Dim i As Integer
i = 0
Do While (i < nRows)
Dim dr As DataRow
dr = dt.NewRow
Dim jj As Integer
jj = 0
Do While (jj < nCols)
dr(jj) = System.String.Format("row {0} col {1}", i, jj)
jj = (jj + 1)
Loop
dt.Rows.Add(dr)
i = (i + 1)
Loop
dt.DefaultView.AllowNew = False
'turn off append row
Return dt
End Function
Private Sub AddCellFormattingColumnStyles(ByVal grid As DataGrid, ByVal handler As FormatCellEventHandler)
Dim ts As DataGridTableStyle
ts = New DataGridTableStyle()
Dim dt As DataTable
dt = CType(grid.DataSource, DataTable)
ts.MappingName = dt.TableName
Dim j As Integer
j = 0
Do While (j < dt.Columns.Count)
Dim cs As DataGridFormattableTextBoxColumn
cs = New DataGridFormattableTextBoxColumn(j)
cs.MappingName = dt.Columns(j).ColumnName
cs.HeaderText = dt.Columns(j).ColumnName
AddHandler cs.SetCellFormat, handler
ts.GridColumnStyles.Add(cs)
j = (j + 1)