473,320 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Custom DataGrid nightmare.

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
Nov 20 '05 #1
4 1805
Hi,

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

Ken
------------------
"Steve" <sf*****@flintind-remove.com> wrote in message
news:OB**************@tk2msftngp13.phx.gbl...
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

Nov 20 '05 #2
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 [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

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

Ken
------------------
"Steve" <sf*****@flintind-remove.com> wrote in message
news:OB**************@tk2msftngp13.phx.gbl...
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


Nov 20 '05 #3
Hi,

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

Ken
--------------
"Steve" <sf*****@flintind-remove.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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 [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

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

Ken
------------------
"Steve" <sf*****@flintind-remove.com> wrote in message
news:OB**************@tk2msftngp13.phx.gbl...
>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
>
>



Nov 20 '05 #4
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 [MVP]" <vb***@bellsouth.net> wrote in message
news:eS****************@TK2MSFTNGP10.phx.gbl...
Hi,

Make sure trash.gif is in the bin directory of your projects folder.
Ken
--------------
"Steve" <sf*****@flintind-remove.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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 [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

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

Ken
------------------
"Steve" <sf*****@flintind-remove.com> wrote in message
news:OB**************@tk2msftngp13.phx.gbl...
>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
>
>



Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Julien | last post by:
Hi, I created a custom server control, compiled as a DLL (it's a composite server control). In my apsx page, I use something like : <MyAssembly:MyObject properties> <asp:datagrid />...
0
by: Stephen | last post by:
This is a real brain-teaser and i'd really appreciate it if someone can try and understand what im trying to do and give me a few pointers or ideas to help me work out my problem. Im basically...
1
by: The_Rave | last post by:
Hi everyone, I'm trying to add my own template columns to the property builder of ..NET. E.g. a checkbox column, or an image column. But I can't find the sources of the wizard, or a way to add...
1
by: Vagabond Software | last post by:
I am creating a custom datagrid based, in part, from someone else's code. The author declared a derived datagrid class in a windows form, then declared a derived ColumnStyle class, in the same form,...
4
by: Dave | last post by:
Hello All, I am having a nightmare trying to add a new row to my Datagrid. When I use the code below I get the error: 'Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount. Can...
2
by: Jay Walker | last post by:
I created a custom DataGridColumn based on Marcie Robillard's MSDN Article: Creating Custom Columns for the ASP.NET Datagrid...
7
by: Girish | last post by:
OK.. phew. Playing with data grids for the past few days has been fun and a huge learning experience.. My problem. I have a requirement to display a gird with a gird. Within the embedded grid,...
1
by: Sam Samnah | last post by:
Hi Everyone. It has been a long time since my last post. Nevertheless, I have built a custom server control that allows a user to edit text, bolding, italics strike though table insertion and...
1
by: rn5a | last post by:
I have created a custom server control which is actually a Button clicking which prompts a user with a JavaScript confirm message asking him whether he would like to proceed or not. If he clicks...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.