Public
Class Form1
Inherits System.Windows.Forms.Form
#
Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
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
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents dataGrid1 As System.Windows.Forms.DataGrid
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
Me.dataGrid1 = New System.Windows.Forms.DataGrid
CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'dataGrid1
'
Me.dataGrid1.DataMember = ""
Me.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.dataGrid1.Location = New System.Drawing.Point(8, 72)
Me.dataGrid1.Name = "dataGrid1"
Me.dataGrid1.Size = New System.Drawing.Size(592, 192)
Me.dataGrid1.TabIndex = 0
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(616, 286)
Me.Controls.Add(Me.dataGrid1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#
End Region
'Public Class Form1
'Inherits System.Windows.Forms.Form
Protected myDataSet As New DataSet
Private strConn = "Data Source=vnis03\tuaba;User ID=sa;Password=blank;Initial Catalog=northwind"
Private strComm = "select * From Customers"
Private sqlConn As System.Data.SqlClient.SqlConnection
Private sqlDA As System.Data.SqlClient.SqlDataAdapter
Private dgts As New DataGridTableStyle
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
sqlConn =
New System.Data.SqlClient.SqlConnection(strConn)
sqlDA =
New System.Data.SqlClient.SqlDataAdapter(strComm, sqlConn)
sqlDA.Fill(
Me.myDataSet, "Cus")
Me.dataGrid1.SetDataBinding(Me.myDataSet, "Cus")
dgts.MappingName = "Cus"
For j As Integer = 0 To Me.myDataSet.Tables("Cus").Columns.Count - 1
Dim textBoxColumn As New System.Windows.Forms.DataGridTextBoxColumn
textBoxColumn.MappingName =
Me.myDataSet.Tables("Cus").Columns(j).ColumnName
textBoxColumn.HeaderText =
Me.myDataSet.Tables("Cus").Columns(j).ColumnName
textBoxColumn.Width = 100
dgts.GridColumnStyles.Add(textBoxColumn)
Next
Me.dataGrid1.TableStyles.Add(dgts)
End Sub
Private Sub PaintCell(ByVal sender As Object, ByVal e As MouseEventArgs)
' Use the HitTest method to get a HitTestInfo object.
Dim hi As DataGrid.HitTestInfo
Dim grid As DataGrid = CType(sender, DataGrid)
hi = grid.HitTest(e.X, e.Y)
' Test if the clicked area was a cell.
If hi.Type = DataGrid.HitTestType.Cell Then
' If it's a cell, get the GridTable and ListManager of the
' clicked table.
Dim dgt As DataGridTableStyle = dataGrid1.TableStyles(0)
Dim cm As CurrencyManager = CType(Me.BindingContext _
(myDataSet.Tables(dgt.MappingName)), CurrencyManager)
' Get the Rectangle of the clicked cell.
Dim cellRect As Rectangle
cellRect = grid.GetCellBounds(hi.Row, hi.Column)
' Get the clicked DataGridTextBoxColumn.
Dim gridCol = CType(dgt.GridColumnStyles(hi.Column), MyGridColumn)==> bugs
'gridCol.GetType()
' get the graphics object for the form.
Dim g As Graphics = dataGrid1.CreateGraphics()
' Create two new Brush objects: a fore brush and back brush.
Dim fBrush As New SolidBrush(Color.Blue)
Dim bBrush As New SolidBrush(Color.Yellow)
' Invoke the Paint method to paint the cell with the brushes.
gridCol.PaintCol(g, cellRect, cm, hi.Row, bBrush, fBrush,
False)
End If
End Sub 'PaintCell
'End Class 'Form1
Private Sub dataGrid1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dataGrid1.MouseDown
Me.PaintCell(sender, e)
End Sub
End
Class
Public
Class MyGridColumn
Inherits System.Windows.Forms.DataGridTextBoxColumn
Public Sub PaintCol(ByVal g As Graphics, ByVal cellRect As Rectangle, _
ByVal cm As CurrencyManager, ByVal rowNum As Integer, ByVal bBrush As Brush, _
ByVal fBrush As Brush, ByVal isVisible As Boolean)
Me.Paint(g, cellRect, cm, rowNum, bBrush, fBrush, isVisible)
End Sub
End
Class
Now, i have problem with statement:
gridCol = CType(dgt.GridColumnStyles(hi.Column), MyGridColumn)
And, when i click a cell, i've delivered message: "Additional information: Specified cast is not valid."