Custom DataGrid nightmare.

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
 
K

Ken Tucker [MVP]

Hi,

Do you have a line like Dim ts as new DatagridTableStyle ?

Ken
------------------
Steve said:
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
 
S

Steve

No, I didn't. I had Dim ts As DataGridTableStyle, but I added new, and I
still got the same problem. Here is the whole datagridcode.
Thank you for looking into this.


AddHandler mycombo2.TextChanged, AddressOf Ctrls_TextChanged2
'OleDb_punch.Fill(Ds1)
DG_punch.Controls.Add(mycombo2)
'------dg_punch TABLESTYLE----------------------
Dim ts As New DataGridTableStyle
DG_punch.DataSource = DS1
DG_punch.DataMember = "main"
ts = New DataGridTableStyle
ts.MappingName = "main"
ts.PreferredRowHeight = 35
ts.AlternatingBackColor = System.Drawing.Color.FromArgb(CType(252,
Byte), CType(253, Byte), CType(206, Byte))
ts.HeaderBackColor = System.Drawing.Color.Navy
ts.HeaderForeColor = System.Drawing.Color.White
'-------dg_punch COLUMNSTYLES----------------------

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)

Dim tb1a As DataGridTextBoxColumn
tb1a = New DataGridTextBoxColumn
' tb1a.HeaderText = ""
tb1a.MappingName = "id"
tb1a.NullText = ""
tb1a.Width = 0
ts.GridColumnStyles.Add(tb1a)

Dim tb1 As DataGridTextBoxColumn
tb1 = New DataGridTextBoxColumn
tb1.HeaderText = "Opened"
tb1.MappingName = "misc"
tb1.NullText = ""
tb1.Format = "MM/dd/yy"
tb1.Width = 75
tb1.ReadOnly = True
ts.GridColumnStyles.Add(tb1)


Dim tb2 As DataGridTextBoxColumn
tb2 = New DataGridTextBoxColumn
tb2.HeaderText = "Item"
tb2.MappingName = "itemno"
tb2.NullText = ""
tb2.Width = 45
tb2.ReadOnly = True
ts.GridColumnStyles.Add(tb2)

Dim tb3 As DataGridTextBoxColumn
tb3 = New DataGridTextBoxColumn
tb3.HeaderText = "Room"
tb3.MappingName = "room_desc"
tb3.NullText = ""
tb3.Width = 120
tb3.ReadOnly = True
ts.GridColumnStyles.Add(tb3)

Dim tb4 As DataGridTextBoxColumnWrap
tb4 = New DataGridTextBoxColumnWrap
tb4.HeaderText = "Item Description"
tb4.MappingName = "text"
tb4.NullText = ""
tb4.Width = 140
tb4.ReadOnly = True
ts.GridColumnStyles.Add(tb4)

Dim tb5 As DataGridTextBoxColumn
tb5 = New DataGridTextBoxColumn
tb5.HeaderText = "Prob"
tb5.MappingName = "problem_abbr"
tb5.NullText = ""
tb5.Width = 50
tb5.ReadOnly = True
ts.GridColumnStyles.Add(tb5)

Dim tb6 As DataGridTextBoxColumn
tb6 = New DataGridTextBoxColumn
tb6.HeaderText = "Resp."
tb6.MappingName = "resp_abbr"
tb6.NullText = ""
tb6.Width = 50
tb6.ReadOnly = True
ts.GridColumnStyles.Add(tb6)

Dim tb7 As DataGridTextBoxColumn
tb7 = New DataGridTextBoxColumn
tb7.HeaderText = "status"
tb7.MappingName = "status"
tb7.NullText = ""
tb7.Width = 70
tb7.ReadOnly = True
ts.GridColumnStyles.Add(tb7)

Dim tb8 As DataGridTextBoxColumn
tb8 = New DataGridTextBoxColumn
tb8.HeaderText = "Closed"
tb8.MappingName = "date_closed"
tb8.Format = "MM/dd/yy"
tb8.NullText = ""
tb8.Width = 75
tb8.ReadOnly = True
ts.GridColumnStyles.Add(tb8)

AddHandler ButtonColStyle.CellButtonClicked, AddressOf
HandleCellButtonClick
AddHandler DG_punch.MouseUp, AddressOf ButtonColStyle.HandleMouseUp
DG_punch.TableStyles.Add(ts)
'----------END TABLESTYLES



Ken Tucker said:
Hi,

Do you have a line like Dim ts as new DatagridTableStyle ?

Ken
------------------
Steve said:
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
 
K

Ken Tucker [MVP]

Hi,

Make sure trash.gif is in the bin directory of your projects folder.

Ken
--------------
Steve said:
No, I didn't. I had Dim ts As DataGridTableStyle, but I added new, and I
still got the same problem. Here is the whole datagridcode.
Thank you for looking into this.


AddHandler mycombo2.TextChanged, AddressOf Ctrls_TextChanged2
'OleDb_punch.Fill(Ds1)
DG_punch.Controls.Add(mycombo2)
'------dg_punch TABLESTYLE----------------------
Dim ts As New DataGridTableStyle
DG_punch.DataSource = DS1
DG_punch.DataMember = "main"
ts = New DataGridTableStyle
ts.MappingName = "main"
ts.PreferredRowHeight = 35
ts.AlternatingBackColor = System.Drawing.Color.FromArgb(CType(252,
Byte), CType(253, Byte), CType(206, Byte))
ts.HeaderBackColor = System.Drawing.Color.Navy
ts.HeaderForeColor = System.Drawing.Color.White
'-------dg_punch COLUMNSTYLES----------------------

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)

Dim tb1a As DataGridTextBoxColumn
tb1a = New DataGridTextBoxColumn
' tb1a.HeaderText = ""
tb1a.MappingName = "id"
tb1a.NullText = ""
tb1a.Width = 0
ts.GridColumnStyles.Add(tb1a)

Dim tb1 As DataGridTextBoxColumn
tb1 = New DataGridTextBoxColumn
tb1.HeaderText = "Opened"
tb1.MappingName = "misc"
tb1.NullText = ""
tb1.Format = "MM/dd/yy"
tb1.Width = 75
tb1.ReadOnly = True
ts.GridColumnStyles.Add(tb1)


Dim tb2 As DataGridTextBoxColumn
tb2 = New DataGridTextBoxColumn
tb2.HeaderText = "Item"
tb2.MappingName = "itemno"
tb2.NullText = ""
tb2.Width = 45
tb2.ReadOnly = True
ts.GridColumnStyles.Add(tb2)

Dim tb3 As DataGridTextBoxColumn
tb3 = New DataGridTextBoxColumn
tb3.HeaderText = "Room"
tb3.MappingName = "room_desc"
tb3.NullText = ""
tb3.Width = 120
tb3.ReadOnly = True
ts.GridColumnStyles.Add(tb3)

Dim tb4 As DataGridTextBoxColumnWrap
tb4 = New DataGridTextBoxColumnWrap
tb4.HeaderText = "Item Description"
tb4.MappingName = "text"
tb4.NullText = ""
tb4.Width = 140
tb4.ReadOnly = True
ts.GridColumnStyles.Add(tb4)

Dim tb5 As DataGridTextBoxColumn
tb5 = New DataGridTextBoxColumn
tb5.HeaderText = "Prob"
tb5.MappingName = "problem_abbr"
tb5.NullText = ""
tb5.Width = 50
tb5.ReadOnly = True
ts.GridColumnStyles.Add(tb5)

Dim tb6 As DataGridTextBoxColumn
tb6 = New DataGridTextBoxColumn
tb6.HeaderText = "Resp."
tb6.MappingName = "resp_abbr"
tb6.NullText = ""
tb6.Width = 50
tb6.ReadOnly = True
ts.GridColumnStyles.Add(tb6)

Dim tb7 As DataGridTextBoxColumn
tb7 = New DataGridTextBoxColumn
tb7.HeaderText = "status"
tb7.MappingName = "status"
tb7.NullText = ""
tb7.Width = 70
tb7.ReadOnly = True
ts.GridColumnStyles.Add(tb7)

Dim tb8 As DataGridTextBoxColumn
tb8 = New DataGridTextBoxColumn
tb8.HeaderText = "Closed"
tb8.MappingName = "date_closed"
tb8.Format = "MM/dd/yy"
tb8.NullText = ""
tb8.Width = 75
tb8.ReadOnly = True
ts.GridColumnStyles.Add(tb8)

AddHandler ButtonColStyle.CellButtonClicked, AddressOf
HandleCellButtonClick
AddHandler DG_punch.MouseUp, AddressOf ButtonColStyle.HandleMouseUp
DG_punch.TableStyles.Add(ts)
'----------END TABLESTYLES



Ken Tucker said:
Hi,

Do you have a line like Dim ts as new DatagridTableStyle ?

Ken
------------------
Steve said:
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
 
S

Steve

Thank You, Thank You, Thank You! It's works great now!

I can not believe that this could cause a crash. For 3 days I have been
trying everything.
You rule!


Steve



Ken Tucker said:
Hi,

Make sure trash.gif is in the bin directory of your projects folder.

Ken
--------------
Steve said:
No, I didn't. I had Dim ts As DataGridTableStyle, but I added new, and I
still got the same problem. Here is the whole datagridcode.
Thank you for looking into this.


AddHandler mycombo2.TextChanged, AddressOf Ctrls_TextChanged2
'OleDb_punch.Fill(Ds1)
DG_punch.Controls.Add(mycombo2)
'------dg_punch TABLESTYLE----------------------
Dim ts As New DataGridTableStyle
DG_punch.DataSource = DS1
DG_punch.DataMember = "main"
ts = New DataGridTableStyle
ts.MappingName = "main"
ts.PreferredRowHeight = 35
ts.AlternatingBackColor = System.Drawing.Color.FromArgb(CType(252,
Byte), CType(253, Byte), CType(206, Byte))
ts.HeaderBackColor = System.Drawing.Color.Navy
ts.HeaderForeColor = System.Drawing.Color.White
'-------dg_punch COLUMNSTYLES----------------------

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)

Dim tb1a As DataGridTextBoxColumn
tb1a = New DataGridTextBoxColumn
' tb1a.HeaderText = ""
tb1a.MappingName = "id"
tb1a.NullText = ""
tb1a.Width = 0
ts.GridColumnStyles.Add(tb1a)

Dim tb1 As DataGridTextBoxColumn
tb1 = New DataGridTextBoxColumn
tb1.HeaderText = "Opened"
tb1.MappingName = "misc"
tb1.NullText = ""
tb1.Format = "MM/dd/yy"
tb1.Width = 75
tb1.ReadOnly = True
ts.GridColumnStyles.Add(tb1)


Dim tb2 As DataGridTextBoxColumn
tb2 = New DataGridTextBoxColumn
tb2.HeaderText = "Item"
tb2.MappingName = "itemno"
tb2.NullText = ""
tb2.Width = 45
tb2.ReadOnly = True
ts.GridColumnStyles.Add(tb2)

Dim tb3 As DataGridTextBoxColumn
tb3 = New DataGridTextBoxColumn
tb3.HeaderText = "Room"
tb3.MappingName = "room_desc"
tb3.NullText = ""
tb3.Width = 120
tb3.ReadOnly = True
ts.GridColumnStyles.Add(tb3)

Dim tb4 As DataGridTextBoxColumnWrap
tb4 = New DataGridTextBoxColumnWrap
tb4.HeaderText = "Item Description"
tb4.MappingName = "text"
tb4.NullText = ""
tb4.Width = 140
tb4.ReadOnly = True
ts.GridColumnStyles.Add(tb4)

Dim tb5 As DataGridTextBoxColumn
tb5 = New DataGridTextBoxColumn
tb5.HeaderText = "Prob"
tb5.MappingName = "problem_abbr"
tb5.NullText = ""
tb5.Width = 50
tb5.ReadOnly = True
ts.GridColumnStyles.Add(tb5)

Dim tb6 As DataGridTextBoxColumn
tb6 = New DataGridTextBoxColumn
tb6.HeaderText = "Resp."
tb6.MappingName = "resp_abbr"
tb6.NullText = ""
tb6.Width = 50
tb6.ReadOnly = True
ts.GridColumnStyles.Add(tb6)

Dim tb7 As DataGridTextBoxColumn
tb7 = New DataGridTextBoxColumn
tb7.HeaderText = "status"
tb7.MappingName = "status"
tb7.NullText = ""
tb7.Width = 70
tb7.ReadOnly = True
ts.GridColumnStyles.Add(tb7)

Dim tb8 As DataGridTextBoxColumn
tb8 = New DataGridTextBoxColumn
tb8.HeaderText = "Closed"
tb8.MappingName = "date_closed"
tb8.Format = "MM/dd/yy"
tb8.NullText = ""
tb8.Width = 75
tb8.ReadOnly = True
ts.GridColumnStyles.Add(tb8)

AddHandler ButtonColStyle.CellButtonClicked, AddressOf
HandleCellButtonClick
AddHandler DG_punch.MouseUp, AddressOf ButtonColStyle.HandleMouseUp
DG_punch.TableStyles.Add(ts)
'----------END TABLESTYLES



Ken Tucker said:
Hi,

Do you have a line like Dim ts as new DatagridTableStyle ?

Ken
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top