S
Steve
I am fairly new to VB.NET, and I am rewriting an application I wrote a while
back, also in VB.NET. I aplied some new things I learned. Anyway, here is my
problem.......
I have a custom DataGrid with a buttonRow that does a delete function for
me. Microsoft support helped me back then to get this done.
Here is part of the code that creates the dataGrid:
Dim ButtonColStyle As DataGridButtonColumn
ButtonColStyle = New DataGridButtonColumn(0) 'pass the column#
ButtonColStyle.MappingName = "ink"
ButtonColStyle.ReadOnly = True
ButtonColStyle.HeaderText = "Delete"
ButtonColStyle.Width = 30
' ts.GridColumnStyles.Add(ButtonColStyle)
When I uncomment the last line, the app crashes and does and I get this
error message:
An unhandled exception of type 'System.NullReferenceException' occurred in
system.windows.forms.dll
Additional information: Object reference not set to an instance of an
object.
Could you please help me out, maybe I just missed something easy.
Here is some more code in case it's needed.
AddHandler ButtonColStyle.CellButtonClicked, AddressOf HandleCellButtonClick
AddHandler DG_punch.MouseUp, AddressOf ButtonColStyle.HandleMouseUp
DG_punch.TableStyles.Add(ts)
Private Sub HandleCellButtonClick(ByVal sender As Object, ByVal e As
DataGridCellButtonClickEventArgs)
If MessageBox.Show("Are you sure you want to delete?", "Delete
Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) =
DialogResult.OK Then
If e.RowIndex < DS1.Tables(0).Rows.Count Then
Dim myCurrencyManager As CurrencyManager =
CType(BindingContext(DG_punch.DataSource, DG_punch.DataMember),
CurrencyManager)
myCurrencyManager.RemoveAt(e.RowIndex)
End If
If e.RowIndex < DS1.Tables(0).Rows.Count Then
DS1.Tables(0).Rows(e.RowIndex).Delete()
End If
OleDb_Punch.Update(DS1)
End If
End Sub
Here is the code of the page that microsoft gave me.......
Option Strict Off
Option Explicit On
Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class DataGridButtonColumn
Inherits DataGridTextBoxColumn
Public Event CellButtonClicked As DataGridCellButtonClickEventHandler
Private _columnNum As Integer
Private _buttonFace As Bitmap
Public Sub New(ByVal colNum As Integer)
_columnNum = colNum
Try
_buttonFace = New Bitmap(Application.StartupPath & "\trash.gif")
Catch
End Try
End Sub 'New
Protected Overloads Overrides Sub Edit(ByVal [source] As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds
As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
As String, ByVal cellIsVisible As Boolean)
End Sub 'Edit
Private Sub DrawButton(ByVal g As Graphics, ByVal bm As Bitmap, ByVal
bounds As Rectangle, ByVal row As Integer)
Dim dg As DataGrid = Me.DataGridTableStyle.DataGrid
g.DrawImage(bm, bounds, 0, 0, bm.Width, bm.Height,
GraphicsUnit.Pixel)
End Sub
Public Sub HandleMouseUp(ByVal sender As Object, ByVal e As
MouseEventArgs)
Dim dg As DataGrid = Me.DataGridTableStyle.DataGrid
Dim hti As DataGrid.HitTestInfo = dg.HitTest(New Point(e.X, e.Y))
Dim isClickInCell As Boolean = (hti.Column = Me._columnNum And
hti.Row > -1)
If Not isClickInCell Then Return
Dim rect As New Rectangle(0, 0, 0, 0)
rect = dg.GetCellBounds(hti.Row, hti.Column)
Dim g As Graphics = Graphics.FromHwnd(dg.Handle)
DrawButton(g, Me._buttonFace, rect, hti.Row)
g.Dispose()
RaiseEvent CellButtonClicked(Me, New
DataGridCellButtonClickEventArgs(hti.Row, hti.Column))
End Sub 'HandleMouseUp
Protected Overloads Overrides Sub Paint(ByVal g As
System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal
[source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer,
ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As
System.Drawing.Brush, ByVal alignToRight As Boolean)
Dim parent As DataGrid = Me.DataGridTableStyle.DataGrid
Dim BackColor As Color
BackColor = parent.AlternatingBackColor
'clear the cell
g.FillRectangle(New SolidBrush(BackColor), bounds)
'draw the value
Dim s As String = Me.GetColumnValueAtRow([source],
rowNum).ToString() 'parent[rowNum, 0].ToString() +
((parent[rowNum, 1].ToString())+ " ").Substring(0,2);
DrawButton(g, _buttonFace, bounds, rowNum)
End Sub 'Paint 'font.Dispose();
End Class 'DataGridButtonColumn
Public Delegate Sub DataGridCellButtonClickEventHandler(ByVal sender As
Object, ByVal e As DataGridCellButtonClickEventArgs)
Public Class DataGridCellButtonClickEventArgs
Inherits EventArgs
Private _row As Integer
Private _col As Integer
Public Sub New(ByVal row As Integer, ByVal col As Integer)
_row = row
_col = col
End Sub 'New
Public ReadOnly Property RowIndex() As Integer
Get
Return _row
End Get
End Property
Public ReadOnly Property ColIndex() As Integer
Get
Return _col
End Get
End Property
End Class
back, also in VB.NET. I aplied some new things I learned. Anyway, here is my
problem.......
I have a custom DataGrid with a buttonRow that does a delete function for
me. Microsoft support helped me back then to get this done.
Here is part of the code that creates the dataGrid:
Dim ButtonColStyle As DataGridButtonColumn
ButtonColStyle = New DataGridButtonColumn(0) 'pass the column#
ButtonColStyle.MappingName = "ink"
ButtonColStyle.ReadOnly = True
ButtonColStyle.HeaderText = "Delete"
ButtonColStyle.Width = 30
' ts.GridColumnStyles.Add(ButtonColStyle)
When I uncomment the last line, the app crashes and does and I get this
error message:
An unhandled exception of type 'System.NullReferenceException' occurred in
system.windows.forms.dll
Additional information: Object reference not set to an instance of an
object.
Could you please help me out, maybe I just missed something easy.
Here is some more code in case it's needed.
AddHandler ButtonColStyle.CellButtonClicked, AddressOf HandleCellButtonClick
AddHandler DG_punch.MouseUp, AddressOf ButtonColStyle.HandleMouseUp
DG_punch.TableStyles.Add(ts)
Private Sub HandleCellButtonClick(ByVal sender As Object, ByVal e As
DataGridCellButtonClickEventArgs)
If MessageBox.Show("Are you sure you want to delete?", "Delete
Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) =
DialogResult.OK Then
If e.RowIndex < DS1.Tables(0).Rows.Count Then
Dim myCurrencyManager As CurrencyManager =
CType(BindingContext(DG_punch.DataSource, DG_punch.DataMember),
CurrencyManager)
myCurrencyManager.RemoveAt(e.RowIndex)
End If
If e.RowIndex < DS1.Tables(0).Rows.Count Then
DS1.Tables(0).Rows(e.RowIndex).Delete()
End If
OleDb_Punch.Update(DS1)
End If
End Sub
Here is the code of the page that microsoft gave me.......
Option Strict Off
Option Explicit On
Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class DataGridButtonColumn
Inherits DataGridTextBoxColumn
Public Event CellButtonClicked As DataGridCellButtonClickEventHandler
Private _columnNum As Integer
Private _buttonFace As Bitmap
Public Sub New(ByVal colNum As Integer)
_columnNum = colNum
Try
_buttonFace = New Bitmap(Application.StartupPath & "\trash.gif")
Catch
End Try
End Sub 'New
Protected Overloads Overrides Sub Edit(ByVal [source] As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds
As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
As String, ByVal cellIsVisible As Boolean)
End Sub 'Edit
Private Sub DrawButton(ByVal g As Graphics, ByVal bm As Bitmap, ByVal
bounds As Rectangle, ByVal row As Integer)
Dim dg As DataGrid = Me.DataGridTableStyle.DataGrid
g.DrawImage(bm, bounds, 0, 0, bm.Width, bm.Height,
GraphicsUnit.Pixel)
End Sub
Public Sub HandleMouseUp(ByVal sender As Object, ByVal e As
MouseEventArgs)
Dim dg As DataGrid = Me.DataGridTableStyle.DataGrid
Dim hti As DataGrid.HitTestInfo = dg.HitTest(New Point(e.X, e.Y))
Dim isClickInCell As Boolean = (hti.Column = Me._columnNum And
hti.Row > -1)
If Not isClickInCell Then Return
Dim rect As New Rectangle(0, 0, 0, 0)
rect = dg.GetCellBounds(hti.Row, hti.Column)
Dim g As Graphics = Graphics.FromHwnd(dg.Handle)
DrawButton(g, Me._buttonFace, rect, hti.Row)
g.Dispose()
RaiseEvent CellButtonClicked(Me, New
DataGridCellButtonClickEventArgs(hti.Row, hti.Column))
End Sub 'HandleMouseUp
Protected Overloads Overrides Sub Paint(ByVal g As
System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal
[source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer,
ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As
System.Drawing.Brush, ByVal alignToRight As Boolean)
Dim parent As DataGrid = Me.DataGridTableStyle.DataGrid
Dim BackColor As Color
BackColor = parent.AlternatingBackColor
'clear the cell
g.FillRectangle(New SolidBrush(BackColor), bounds)
'draw the value
Dim s As String = Me.GetColumnValueAtRow([source],
rowNum).ToString() 'parent[rowNum, 0].ToString() +
((parent[rowNum, 1].ToString())+ " ").Substring(0,2);
DrawButton(g, _buttonFace, bounds, rowNum)
End Sub 'Paint 'font.Dispose();
End Class 'DataGridButtonColumn
Public Delegate Sub DataGridCellButtonClickEventHandler(ByVal sender As
Object, ByVal e As DataGridCellButtonClickEventArgs)
Public Class DataGridCellButtonClickEventArgs
Inherits EventArgs
Private _row As Integer
Private _col As Integer
Public Sub New(ByVal row As Integer, ByVal col As Integer)
_row = row
_col = col
End Sub 'New
Public ReadOnly Property RowIndex() As Integer
Get
Return _row
End Get
End Property
Public ReadOnly Property ColIndex() As Integer
Get
Return _col
End Get
End Property
End Class