NullReferenceException when delete last row in a datagrid

R

Ryan Liu

Hi there,

I got a NullReferenceException when delete last row in a datagrid.

I had hard time to solve since it does not occur in my own code.

I put a datagrid in my inherited user control, then put this control on a
form.

I use DataAdaptor to fill the data table and update database.

I have no problem to delete rows in datagrid, unless it is last row.

If I do delete the last row, use Delete key on my keyboard, it throws
following exception.

Is this a known bug? Or can someone point out the clue I can look at? Since
the delet is not handled by myself, I really don't know how to solve it.

Thanks a lot!
Ryan

System.NullReferenceException: Object reference not set to an instance of an
object.
at System.Windows.Forms.DataGrid.ResetSelection()
at System.Windows.Forms.DataGrid.ResetUIState()
at System.Windows.Forms.DataGrid.SetDataGridRows(DataGridRow[] newRows,
Int32 newRowsLength)
at System.Windows.Forms.DataGrid.DeleteDataGridRows(Int32 deletedRows)
at System.Windows.Forms.DataGrid.DeleteRows(DataGridRow[] localGridRows)
at System.Windows.Forms.DataGrid.ProcessGridKey(KeyEventArgs ke)
at System.Windows.Forms.DataGrid.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.Control.PreProcessMessage(Message& msg)
at
System.Windows.Forms.ThreadContext.System.Windows.Forms.UnsafeNativeMethods+IMsoComponent.FPreTranslateMessage(MSG&
msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
 
R

Ryan Liu

Hi Brian,

Have you found any problem with my code? Do you need more source code from
me?

Thanks a lot!
Ryan


Ryan Liu said:
Hi Brian,

Thanks for your help. I tied to send email to you from 3 mail server but
all failed. I have to post to this group again.

Here are the files related.

a.. SelfDefineAbnormalEndGridControl inherits RefeshDataGeridControl
b.. RefeshDataGeridControl use SysUtil to read and upate database.
c.. RefeshDataGeridControl is put on SystemManager form, and user can
edit, delete data.

All these files are attached. I will high light the codes here.


a.. SysUtil.cs public static MySqlDataAdapter
BuildDbAdapterForSelfDefindedAbnormalEnd(IDbConnection conn){} return a
adapter to read and write back to db.

b.. SelfDefineAbnormalEndGridControl implements RefeshDataGeridControl's
RefreshDataImp() function to read data from db.

c.. SystemManager is the form user can interactive with. It has 3 tab
pages and quite some code. But for our discussion, it is simple, just a
selfDefineAbnormalEndGridControl1 on it, and when form is load,
SelfDefineAbnormalEndGridControl UpdateDb() method is called.

You see, when user press Delete key on keyboard, this is function of
datagrid, not control by my code. And it throw an exception when the user
delete the last row in datagrid.

Thanks a lot for your help!
Ryan




Brian Delahunty said:
Can you post your code please.

--
Brian Delahunty
Ireland

http://briandela.com/blog

INDA SouthEast - http://southeast.developers.ie/ - The .NET usergroup I
started in the southeast of Ireland.


Ryan Liu said:
Hi there,

I got a NullReferenceException when delete last row in a datagrid.

I had hard time to solve since it does not occur in my own code.

I put a datagrid in my inherited user control, then put this control on
a
form.

I use DataAdaptor to fill the data table and update database.

I have no problem to delete rows in datagrid, unless it is last row.

If I do delete the last row, use Delete key on my keyboard, it throws
following exception.

Is this a known bug? Or can someone point out the clue I can look at?
Since
the delet is not handled by myself, I really don't know how to solve it.

Thanks a lot!
Ryan

System.NullReferenceException: Object reference not set to an instance
of an
object.
at System.Windows.Forms.DataGrid.ResetSelection()
at System.Windows.Forms.DataGrid.ResetUIState()
at System.Windows.Forms.DataGrid.SetDataGridRows(DataGridRow[]
newRows,
Int32 newRowsLength)
at System.Windows.Forms.DataGrid.DeleteDataGridRows(Int32
deletedRows)
at System.Windows.Forms.DataGrid.DeleteRows(DataGridRow[]
localGridRows)
at System.Windows.Forms.DataGrid.ProcessGridKey(KeyEventArgs ke)
at System.Windows.Forms.DataGrid.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.Control.PreProcessMessage(Message& msg)
at
System.Windows.Forms.ThreadContext.System.Windows.Forms.UnsafeNativeMethods+IMsoComponent.FPreTranslateMessage(MSG&
msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32
reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
 
R

Ryan Liu

Proiblen solved, all because I add CurrentCellChanged event, in which, I
select whole row. When I remove it, it is fine.

/// <summary>
/// automatically select whole row
/// make it protected so when the class who inherits it can remove it
by -=
/// this is necessary when child allow delete the last row
/// then this event itself will not fire exception, but it will cause
datagrid internal exception
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void dataGrid_CurrentCellChanged(object sender, EventArgs e)
{
if(this.dataGrid.CurrentRowIndex != -1) //not empty table
{
this.dataGrid.Select(this.dataGrid.CurrentRowIndex);
}
}


Ryan Liu said:
Hi Brian,

Have you found any problem with my code? Do you need more source code from
me?

Thanks a lot!
Ryan


Ryan Liu said:
Hi Brian,

Thanks for your help. I tied to send email to you from 3 mail server but
all failed. I have to post to this group again.

Here are the files related.

a.. SelfDefineAbnormalEndGridControl inherits RefeshDataGeridControl
b.. RefeshDataGeridControl use SysUtil to read and upate database.
c.. RefeshDataGeridControl is put on SystemManager form, and user can
edit, delete data.

All these files are attached. I will high light the codes here.


a.. SysUtil.cs public static MySqlDataAdapter
BuildDbAdapterForSelfDefindedAbnormalEnd(IDbConnection conn){} return a
adapter to read and write back to db.

b.. SelfDefineAbnormalEndGridControl implements
RefeshDataGeridControl's RefreshDataImp() function to read data from db.

c.. SystemManager is the form user can interactive with. It has 3 tab
pages and quite some code. But for our discussion, it is simple, just a
selfDefineAbnormalEndGridControl1 on it, and when form is load,
SelfDefineAbnormalEndGridControl UpdateDb() method is called.

You see, when user press Delete key on keyboard, this is function of
datagrid, not control by my code. And it throw an exception when the user
delete the last row in datagrid.

Thanks a lot for your help!
Ryan




Brian Delahunty said:
Can you post your code please.

--
Brian Delahunty
Ireland

http://briandela.com/blog

INDA SouthEast - http://southeast.developers.ie/ - The .NET usergroup I
started in the southeast of Ireland.


:

Hi there,

I got a NullReferenceException when delete last row in a datagrid.

I had hard time to solve since it does not occur in my own code.

I put a datagrid in my inherited user control, then put this control on
a
form.

I use DataAdaptor to fill the data table and update database.

I have no problem to delete rows in datagrid, unless it is last row.

If I do delete the last row, use Delete key on my keyboard, it throws
following exception.

Is this a known bug? Or can someone point out the clue I can look at?
Since
the delet is not handled by myself, I really don't know how to solve
it.

Thanks a lot!
Ryan

System.NullReferenceException: Object reference not set to an instance
of an
object.
at System.Windows.Forms.DataGrid.ResetSelection()
at System.Windows.Forms.DataGrid.ResetUIState()
at System.Windows.Forms.DataGrid.SetDataGridRows(DataGridRow[]
newRows,
Int32 newRowsLength)
at System.Windows.Forms.DataGrid.DeleteDataGridRows(Int32
deletedRows)
at System.Windows.Forms.DataGrid.DeleteRows(DataGridRow[]
localGridRows)
at System.Windows.Forms.DataGrid.ProcessGridKey(KeyEventArgs ke)
at System.Windows.Forms.DataGrid.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.Control.PreProcessMessage(Message& msg)
at
System.Windows.Forms.ThreadContext.System.Windows.Forms.UnsafeNativeMethods+IMsoComponent.FPreTranslateMessage(MSG&
msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32
reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
 

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