Weird Error

  • Thread starter Thread starter scorpion53061
  • Start date Start date
S

scorpion53061

This exception occured while running in the development environment. The
debugger didnt catch it though it appeared as a message box
prompt........does anyone have insight on this?


************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an
object.
at System.Windows.Forms.DataGrid.AddNewRow()
at System.Windows.Forms.DataGridAddNewRow.OnEdit()
at System.Windows.Forms.DataGrid.Edit(String instantText)
at System.Windows.Forms.DataGrid.Edit()
at System.Windows.Forms.DataGrid.OnEnter(EventArgs e)
at System.Windows.Forms.Control.NotifyEnter()
at System.Windows.Forms.ContainerControl.UpdateFocusedControl()
 
Looks like you are trying to pass add a row to your datatable but the row
must not reference a row object. You probably forgot to use 'New'.


System.Windows.Forms.DataGrid.AddNewRow()


Regards - OHM#


This exception occured while running in the development environment.
The debugger didnt catch it though it appeared as a message box
prompt........does anyone have insight on this?


************** Exception Text **************
System.NullReferenceException: Object reference not set to an
instance of an object.
at System.Windows.Forms.DataGrid.AddNewRow()
at System.Windows.Forms.DataGridAddNewRow.OnEdit()
at System.Windows.Forms.DataGrid.Edit(String instantText)
at System.Windows.Forms.DataGrid.Edit()
at System.Windows.Forms.DataGrid.OnEnter(EventArgs e)
at System.Windows.Forms.Control.NotifyEnter()
at System.Windows.Forms.ContainerControl.UpdateFocusedControl()

Regards - OHM# (e-mail address removed)
 
That is the thing though......

I set hte grid to be read only in the designer. You can't add a row.
 
Either send the code zipped and I'll try it or paste the relevant code here.
You ARE running code which is causing this error, it's possibly in the
Form_Load event

OHM#
That is the thing though......

I set hte grid to be read only in the designer. You can't add a row.

Regards - OHM# (e-mail address removed)
 
I just emailed you.

One Handed Man said:
Either send the code zipped and I'll try it or paste the relevant code here.
You ARE running code which is causing this error, it's possibly in the
Form_Load event

OHM#


Regards - OHM# (e-mail address removed)
 
2 datagrids with 2 click events and 2 resizing and column styles.

THe error occurs after clicking in the grid and then going to click a
button, it does not matter which one.

Thanks again.........





Private Sub ItemList_CurrentCellChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles

DataGrid1.CurrentCellChanged
Dim itemcheck As Integer
Dim foundit As Boolean
Dim chitem1 As String
chitem1 = DataGrid1.Item(DataGrid1.CurrentCell)
For itemcheck = 0 To dscopy.Tables(0).Rows.Count - 1
If chitem1 =
dscopy.Tables(0).Rows(itemcheck).Item(newcol.ColumnName) Then
chitem = DataGrid1.Item(DataGrid1.CurrentCell)
Label2.Text = ""
Label18.Text = ""
Label14.Text = ""
Label7.Text = ""
Label16.Text = ""
Label11.Text = ""
TextBox1.Text = ""
Label6.Text = ""
desctext =
dscopy.Tables(0).Rows(itemcheck).Item(newcol1.ColumnName)
foundit = True
Label3.Text = chitem
EnableFunctions()
'Button1.Focus()
DataGrid1.Refresh()
foundit = True
AssignValues()
Exit For
Exit Sub
End If
Next
If foundit = False Then
Exit Sub
End If

End Sub
'datagrid for reports
Private Sub ItemList2_CurrentCellChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles

DataGrid2.CurrentCellChanged
Dim itemcheck As Integer
Dim foundit As Boolean
Dim chitem1 As String
chitem1 = DataGrid2.Item(DataGrid2.CurrentCell)
For itemcheck = 0 To dscopy.Tables(0).Rows.Count - 1
If chitem1 =
dscopy.Tables(0).Rows(itemcheck).Item(newcol.ColumnName) Then
chitem = DataGrid2.Item(DataGrid2.CurrentCell)
Label2.Text = ""
Label18.Text = ""
Label14.Text = ""
Label7.Text = ""
Label16.Text = ""
Label11.Text = ""
TextBox1.Text = ""
Label6.Text = ""
desctext =
dscopy.Tables(0).Rows(itemcheck).Item(newcol1.ColumnName)
foundit = True
Label3.Text = chitem.ToString
EnableFunctions()
'Button1.Focus()
DataGrid2.Refresh()
foundit = True
AssignValues()
Exit For
Exit Sub
End If
Next
If foundit = False Then
Exit Sub
End If

End Sub



Public Sub AddSizedColoredColumns2()
DataGrid1.TableStyles.Clear()
'DataGrid2.TableStyles.Clear()

Dim Artwork1 As New DataGridTableStyle
Artwork1.MappingName = dscopy.Tables(0).TableName '"FAVORITEITEMS"
If TabControl1.SelectedIndex = 2 Then
Dim BOOKSLABEL As New DatagridFormat
BOOKSLABEL.TextBox.BackColor = Color.SpringGreen
BOOKSLABEL.MappingName = "ITEMNO"
BOOKSLABEL.HeaderText = "ITEM"
'BOOKSLABEL.Width = 200
BOOKSLABEL.TextBox.ReadOnly = True
BOOKSLABEL.NullText = "VALUE"

Dim ANSWERGRID As New DatagridFormat
ANSWERGRID.MappingName = "DESCRIPTION"
ANSWERGRID.HeaderText = "DESC"
'ANSWERGRID.Width = 300
ANSWERGRID.NullText = "VALUE"
ANSWERGRID.Alignment = HorizontalAlignment.Left

Dim ANSWERGRID1 As New DatagridFormat
ANSWERGRID1.MappingName = "UM"
ANSWERGRID1.HeaderText = "UM"
'ANSWERGRID1.Width = 50
ANSWERGRID1.Alignment = HorizontalAlignment.Left
ANSWERGRID1.NullText = "NONE RECORDED"
ANSWERGRID1.TextBox.ReadOnly = True

Dim ANSWERGRID2 As New DatagridFormat
ANSWERGRID2.MappingName = "TYPE"
ANSWERGRID2.HeaderText = "TYPE"
'ANSWERGRID2.Width = 200
'ANSWERGRID2.Width = 200
ANSWERGRID2.Alignment = HorizontalAlignment.Left
ANSWERGRID2.NullText = "VALUE"
ANSWERGRID2.TextBox.ReadOnly = True

Artwork1.GridColumnStyles.Add(ANSWERGRID)
Artwork1.GridColumnStyles.Add(BOOKSLABEL)
Artwork1.GridColumnStyles.Add(ANSWERGRID1)
Artwork1.GridColumnStyles.Add(ANSWERGRID2)
Artwork1.PreferredRowHeight = 40
Artwork1.GridColumnStyles.Item(0).NullText = ""
Artwork1.GridLineColor =
System.Drawing.SystemColors.ControlDarkDark
Artwork1.GridLineStyle = DataGridLineStyle.Solid
Artwork1.HeaderBackColor = System.Drawing.Color.DarkBlue
Artwork1.HeaderForeColor =
System.Drawing.SystemColors.ActiveCaptionText
Artwork1.RowHeadersVisible = False
Artwork1.AllowSorting = False
Artwork1.BackColor =
System.Drawing.SystemColors.ActiveCaptionText
'AutoSizeTable()
Dim numCols As Integer
numCols = dscopy.Tables(0).Columns.Count
Dim i As Integer
i = 0
Do While (i < numCols)
AutoSizeCol(i)
i = (i + 1)
Loop
DataGrid1.TableStyles.Add(Artwork1)
DataGrid1.ReadOnly = True
MsgBox(numCols & " is numcols" & vbCrLf &
dscopy.Tables(0).Columns.Count)
Exit Sub
End If

'additional pages setup


End Sub

Public Sub AddSizedColoredColumns3()
DataGrid2.TableStyles.Clear()
If typeofreport = "OPEN" Then
'DataGrid2.TableStyles.Clear()

Dim OpenStyle As New DataGridTableStyle
OpenStyle.MappingName = dsreportscopy.Tables(0).TableName
'"FAVORITEITEMS"
Dim OpenItemNo As New DatagridFormat
OpenItemNo.TextBox.BackColor = Color.SpringGreen
OpenItemNo.MappingName = "ITEMNO"
OpenItemNo.HeaderText = "ITEM"
'OpenItemNo.Width = 200
OpenItemNo.TextBox.ReadOnly = True
OpenItemNo.NullText = "VALUE"

Dim OpenDateOr As New DatagridFormat
OpenDateOr.MappingName = "dateor"
OpenDateOr.HeaderText = "DATE"
'OpenDateOr.Width = 300
OpenDateOr.NullText = "VALUE"
OpenDateOr.Alignment = HorizontalAlignment.Left

Dim OpenCustpo As New DatagridFormat
OpenCustpo.MappingName = "custpo"
OpenCustpo.HeaderText = "PO/JOB#"
'OpenCustpo.Width = 50
OpenCustpo.Alignment = HorizontalAlignment.Left
OpenCustpo.NullText = "NONE RECORDED"
OpenCustpo.TextBox.ReadOnly = True

Dim OpenORDERNO As New DatagridFormat
OpenORDERNO.MappingName = "ORDERNO"
OpenORDERNO.HeaderText = "JJK#"
'OpenORDERNO.Width = 200
'OpenORDERNO.Width = 200
OpenORDERNO.Alignment = HorizontalAlignment.Left
OpenORDERNO.NullText = "VALUE"
OpenORDERNO.TextBox.ReadOnly = True

Dim OPENLN As New DatagridFormat
OPENLN.MappingName = "LN"
OPENLN.HeaderText = "LINE"
'OPENLN.Width = 200
'OPENLN.Width = 200
OPENLN.Alignment = HorizontalAlignment.Left
OPENLN.NullText = "VALUE"
OPENLN.TextBox.ReadOnly = True

Dim OPENPRICE As New DatagridFormat
OPENPRICE.MappingName = "PRICE"
OPENPRICE.HeaderText = "COST"
'OPENPRICE.Width = 200
'OPENPRICE.Width = 200
OPENPRICE.Alignment = HorizontalAlignment.Left
OPENPRICE.NullText = "VALUE"
OPENPRICE.TextBox.ReadOnly = True

Dim OPENUM As New DatagridFormat
OPENUM.MappingName = "UM"
OPENUM.HeaderText = "UM"
'OPENUM.Width = 200
'OPENUM.Width = 200
OPENUM.Alignment = HorizontalAlignment.Left
OPENUM.NullText = "VALUE"
OPENUM.TextBox.ReadOnly = True
Dim OPENtotprice As New DatagridFormat
OPENtotprice.MappingName = "totprice"
OPENtotprice.HeaderText = "TOTAL"
'OPENtotprice.Width = 200
'OPENtotprice.Width = 200
OPENtotprice.Alignment = HorizontalAlignment.Left
OPENtotprice.NullText = "VALUE"
OPENtotprice.TextBox.ReadOnly = True

Dim OPENQTYALL As New DatagridFormat
OPENQTYALL.MappingName = "QTYALL"
OPENQTYALL.HeaderText = "ALLOC"
'OPENQTYALL.Width = 200
'OPENQTYALL.Width = 200
OPENQTYALL.Alignment = HorizontalAlignment.Left
OPENQTYALL.NullText = "VALUE"
OPENQTYALL.TextBox.ReadOnly = True

Dim OPENQTYBACK As New DatagridFormat
OPENQTYBACK.MappingName = "QTYBACK"
OPENQTYBACK.HeaderText = "BACK"
'OPENQTYBACK.Width = 200
'OPENQTYBACK.Width = 200
OPENQTYBACK.Alignment = HorizontalAlignment.Left
OPENQTYBACK.NullText = "VALUE"
OPENQTYBACK.TextBox.ReadOnly = True

Dim OPENQTY As New DatagridFormat
OPENQTY.MappingName = "QTY"
OPENQTY.HeaderText = "QTY"
'OPENQTY.Width = 200
'OPENQTY.Width = 200
OPENQTY.Alignment = HorizontalAlignment.Left
OPENQTY.NullText = "VALUE"
OPENQTY.TextBox.ReadOnly = True

OpenStyle.GridColumnStyles.Add(OpenDateOr)
OpenStyle.GridColumnStyles.Add(OpenItemNo)
OpenStyle.GridColumnStyles.Add(OpenCustpo)
OpenStyle.GridColumnStyles.Add(OpenORDERNO)
OpenStyle.GridColumnStyles.Add(OPENLN)
OpenStyle.GridColumnStyles.Add(OPENPRICE)
OpenStyle.GridColumnStyles.Add(OPENUM)
OpenStyle.GridColumnStyles.Add(OPENQTY)
OpenStyle.GridColumnStyles.Add(OPENtotprice)
OpenStyle.GridColumnStyles.Add(OPENQTYALL)
OpenStyle.GridColumnStyles.Add(OPENQTYBACK)

OpenStyle.PreferredRowHeight = 40
OpenStyle.GridColumnStyles.Item(0).NullText = ""
OpenStyle.GridLineColor =
System.Drawing.SystemColors.ControlDarkDark
OpenStyle.GridLineStyle = DataGridLineStyle.Solid
OpenStyle.HeaderBackColor = System.Drawing.Color.DarkBlue
OpenStyle.HeaderForeColor =
System.Drawing.SystemColors.ActiveCaptionText
OpenStyle.RowHeadersVisible = False
OpenStyle.AllowSorting = False
OpenStyle.BackColor =
System.Drawing.SystemColors.ActiveCaptionText
'AutoSizeTable()
Dim numCols As Integer
numCols = dsreportscopy.Tables(0).Columns.Count
Dim i As Integer
i = 0
Do While (i < numCols)
AutoSizeCol2(i)
i = (i + 1)
Loop
DataGrid2.TableStyles.Add(OpenStyle)
DataGrid2.ReadOnly = True
Exit Sub
End If
'additional pages setup

End Sub

Public Sub AutoSizeTable()

Dim numCols As Integer
numCols = dscopy.Tables(0).Columns.Count
'numCols = CType(DataGrid1.DataSource, DataTable).Columns.Count
Dim i As Integer
i = 0

Do While (i < numCols)
AutoSizeCol(i)
i = (i + 1)

Loop

End Sub

Public Sub AutoSizeCols(ByVal start As Integer, ByVal finish As Integer)

Dim i As Integer
i = start

Do While (i <= finish)
AutoSizeCol(i)
i = (i + 1)

Loop

End Sub

Public Sub AutoSizeCol(ByVal col As Integer)

Dim width As Single
width = 0
Dim numRows As Integer
numRows = dscopy.Tables(0).Rows.Count
'numRows = CType(DataGrid1.DataSource, DataTable).Rows.Count
Dim g As Graphics
g = Graphics.FromHwnd(DataGrid1.Handle)
Dim sf As StringFormat
sf = New StringFormat(StringFormat.GenericTypographic)
Dim size As SizeF
Dim i As Integer
i = 0

Do While (i < numRows)
size = g.MeasureString(DataGrid1(i, col).ToString,
DataGrid1.Font, 500, sf)
If (size.Width > width) Then
width = size.Width
End If
i = (i + 1)

Loop
'DataGrid1.Width = (size.Width * dscopy.Tables(0).Columns.Count + 1)
* dscopy.Tables(0).Columns.Count + 1


g.Dispose()
'
DataGrid1.TableStyles(dscopy.Tables(0).TableName).GridColumnStyles(col).Widt
h = CType(width, Integer)

End Sub

Public Sub AutoSizeTable2()

Dim numCols As Integer
numCols = dsreportscopy.Tables(0).Columns.Count
'numCols = CType(DataGrid1.DataSource, DataTable).Columns.Count
Dim i As Integer
i = 0

Do While (i < numCols)
AutoSizeCol2(i)
i = (i + 1)

Loop

End Sub

Public Sub AutoSizeCols2(ByVal start As Integer, ByVal finish As
Integer)

Dim i As Integer
i = start

Do While (i <= finish)
AutoSizeCol2(i)
i = (i + 1)

Loop

End Sub

Public Sub AutoSizeCol2(ByVal col As Integer)

Dim width As Single
width = 0
Dim numRows As Integer
numRows = dsreportscopy.Tables(0).Rows.Count
'numRows = CType(datagrid2.DataSource, DataTable).Rows.Count
Dim g As Graphics
g = Graphics.FromHwnd(DataGrid2.Handle)
Dim sf As StringFormat
sf = New StringFormat(StringFormat.GenericTypographic)
Dim size As SizeF
Dim i As Integer
i = 0

Do While (i < numRows)
size = g.MeasureString(DataGrid2(i, col).ToString,
DataGrid2.Font, 500, sf)
If (size.Width > width) Then
width = size.Width
End If
i = (i + 1)

Loop
'datagrid2.Width = (size.Width *
dsreportscopy.Tables(0).Columns.Count + 1) *
dsreportscopy.Tables(0).Columns.Count

+ 1


g.Dispose()
'
datagrid2.TableStyles(dsreportscopy.Tables(0).TableName).GridColumnStyles(co
l).Width = CType(width, Integer)

End Sub
 
Thanks but the code is incomplete and thefore I cannot run it. Please send
entire form.

Regards - OHM#

I just emailed you.

Regards - OHM# (e-mail address removed)
 
Ok but that is going to take a bit as I have to pull all the SQL connections
and change them to OleDb(Access) to prevent password problems.
 
Back
Top