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) Thencomponents.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#
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.LoadsqlConn =
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.DataGridTextBoxColumntextBoxColumn.MappingName =
Me.myDataSet.Tables("Cus").Columns(j).ColumnNametextBoxColumn.HeaderText =
Me.myDataSet.Tables("Cus").Columns(j).ColumnNametextBoxColumn.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 RectanglecellRect = 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 SubEnd
ClassPublic
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 SubEnd
ClassNow, 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."
Jayadev NairPosted Apr 1, 2006, 5:59 AM
The error is raised because you tried to typecast a DatagridColumnStyle to DatagridTextBoxColumn.
Dim gridCol = CType(dgt.GridColumnStyles(hi.Column), MyGridColumn)
dgt.GridColumnStyles(hi.Column) Is of Type DatagridColumnStyle
MyGridColumn Is of Type DatagridTextBoxColumn
If you need column, you could get it from dgt.datagrid.columns(XXX), isnt it?
Thanks.