Hello Xtreme - my first post: please be kind.
My application has a read-only datagridview dgvFound fed from a datatable dtFound. This datagridview is permanently displayed whilst the application is running.
dtFound is declared public in a module (and exists whilst the application is running).
dtFound is fed from any one of around thirty different "finders" that are grouped in three user controls - these finders run sprocs on MSSQL2005 and clear/refill dtFound.
Everything works perfectly with dgvFound refreshing nicely in the main form when one of the finders raises a dtFoundIsReady event...
... until the user clicks the header of dgvFound to sort. After the sort everything goes pear-shaped with a NullReferenceException.
Some code:
and then in the event handler:
The users can live without sorting - so for the time being I have simply turned sort off, but I would like to understand what is happening here, and how I might be able to leave sorting on.
Chris
My application has a read-only datagridview dgvFound fed from a datatable dtFound. This datagridview is permanently displayed whilst the application is running.
dtFound is declared public in a module (and exists whilst the application is running).
dtFound is fed from any one of around thirty different "finders" that are grouped in three user controls - these finders run sprocs on MSSQL2005 and clear/refill dtFound.
Everything works perfectly with dgvFound refreshing nicely in the main form when one of the finders raises a dtFoundIsReady event...
... until the user clicks the header of dgvFound to sort. After the sort everything goes pear-shaped with a NullReferenceException.
Some code:
Code:
...yada yada yada to prepare cmd
'// exec the sproc
conn1.Open()
Dim oDr As SqlDataReader = cmd.ExecuteReader()
'// clear any entries in existing datatable dtFound
dtFound.Rows.Clear()
dtFound.Columns.Clear()
dtFound.Clear()
'// refill dtFound
Dim i As Integer
Dim count As Integer = oDr.FieldCount - 1
'//add columns
For i = 0 To count
dtFound.Columns.Add(oDr.GetName(i), oDr.GetFieldType(i))
Next
'//add rows
Do While oDr.Read()
Dim r As DataRow = dtFound.NewRow
For i = 0 To count
r(i) = oDr.Item(i)
Next [B][COLOR="Red"]
'NullReferenceException on next line if dgvFound has been sorted by user [/COLOR]
dtFound.Rows.Add(r) [/B]
Loop
'// raise an event to let the application know dtFound is filled
Code:
Me.dgvFound.DataSource = dtFound
'// WORKAROUND to evade NullReferenceException after sort
Dim i As Integer
For i = 0 To dgvFound.ColumnCount - 1
Me.dgvFound.Columns(i).SortMode = DataGridViewColumnSortMode.NotSortable
Next
The users can live without sorting - so for the time being I have simply turned sort off, but I would like to understand what is happening here, and how I might be able to leave sorting on.
Chris