472,992 Members | 3,218 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

NullReferenceException when delete last row in a datagrid

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(Data GridRow[] newRows,
Int32 newRowsLength)
at System.Windows.Forms.DataGrid.DeleteDataGridRows(I nt32 deletedRows)
at System.Windows.Forms.DataGrid.DeleteRows(DataGridR ow[] localGridRows)
at System.Windows.Forms.DataGrid.ProcessGridKey(KeyEv entArgs ke)
at System.Windows.Forms.DataGrid.ProcessDialogKey(Key s keyData)
at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.TextBoxBase.ProcessDialogKey( Keys keyData)
at System.Windows.Forms.Control.PreProcessMessage(Mes sage& msg)
at
System.Windows.Forms.ThreadContext.System.Windows. Forms.UnsafeNativeMethods+IMsoComponent.FPreTransl ateMessage(MSG&
msg)
at
System.Windows.Forms.ComponentManager.System.Windo ws.Forms.UnsafeNativeMethods+IMsoComponentManager. FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopI nner(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop( Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
Nov 17 '05 #1
3 4980
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" wrote:
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(Data GridRow[] newRows,
Int32 newRowsLength)
at System.Windows.Forms.DataGrid.DeleteDataGridRows(I nt32 deletedRows)
at System.Windows.Forms.DataGrid.DeleteRows(DataGridR ow[] localGridRows)
at System.Windows.Forms.DataGrid.ProcessGridKey(KeyEv entArgs ke)
at System.Windows.Forms.DataGrid.ProcessDialogKey(Key s keyData)
at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.TextBoxBase.ProcessDialogKey( Keys keyData)
at System.Windows.Forms.Control.PreProcessMessage(Mes sage& msg)
at
System.Windows.Forms.ThreadContext.System.Windows. Forms.UnsafeNativeMethods+IMsoComponent.FPreTransl ateMessage(MSG&
msg)
at
System.Windows.Forms.ComponentManager.System.Windo ws.Forms.UnsafeNativeMethods+IMsoComponentManager. FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopI nner(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop( Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)

Nov 17 '05 #2
Hi Brian,

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

Thanks a lot!
Ryan
"Ryan Liu" <rl**@eSpaceSoft.com> 写入消息新闻:OG**************@tk2msftngp13.phx.gbl ...
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(IDbConnec tion 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" <Br************@discussions.microsoft.com> 写入消息新闻:89**********************************@ microsoft.com...
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" wrote:
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(Data GridRow[]
newRows,
Int32 newRowsLength)
at System.Windows.Forms.DataGrid.DeleteDataGridRows(I nt32
deletedRows)
at System.Windows.Forms.DataGrid.DeleteRows(DataGridR ow[]
localGridRows)
at System.Windows.Forms.DataGrid.ProcessGridKey(KeyEv entArgs ke)
at System.Windows.Forms.DataGrid.ProcessDialogKey(Key s keyData)
at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.TextBoxBase.ProcessDialogKey( Keys keyData)
at System.Windows.Forms.Control.PreProcessMessage(Mes sage& msg)
at
System.Windows.Forms.ThreadContext.System.Windows. Forms.UnsafeNativeMethods+IMsoComponent.FPreTransl ateMessage(MSG&
msg)
at
System.Windows.Forms.ComponentManager.System.Windo ws.Forms.UnsafeNativeMethods+IMsoComponentManager. FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopI nner(Int32
reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop( Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)


Nov 17 '05 #3
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" <rl**@eSpaceSoft.com> 写入消息新闻:Oo**************@TK2MSFTNGP09.phx.gbl ...
Hi Brian,

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

Thanks a lot!
Ryan
"Ryan Liu" <rl**@eSpaceSoft.com> 写入消息新闻:OG**************@tk2msftngp13.phx.gbl ...
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(IDbConnec tion 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" <Br************@discussions.microsoft.com> 写入消息新闻:89**********************************@ microsoft.com...
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" wrote:

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(Data GridRow[]
newRows,
Int32 newRowsLength)
at System.Windows.Forms.DataGrid.DeleteDataGridRows(I nt32
deletedRows)
at System.Windows.Forms.DataGrid.DeleteRows(DataGridR ow[]
localGridRows)
at System.Windows.Forms.DataGrid.ProcessGridKey(KeyEv entArgs ke)
at System.Windows.Forms.DataGrid.ProcessDialogKey(Key s keyData)
at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.TextBoxBase.ProcessDialogKey( Keys keyData)
at System.Windows.Forms.Control.PreProcessMessage(Mes sage& msg)
at
System.Windows.Forms.ThreadContext.System.Windows. Forms.UnsafeNativeMethods+IMsoComponent.FPreTransl ateMessage(MSG&
msg)
at
System.Windows.Forms.ComponentManager.System.Windo ws.Forms.UnsafeNativeMethods+IMsoComponentManager. FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopI nner(Int32
reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop( Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)



Nov 17 '05 #4

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

Similar topics

3
by: Terrence | last post by:
I am doing some of the C# walkthroughs to transition from VB to C#. When I try to execute static void Main() { Aplication.Run(new Form1()) } I raise a 'System.NullReferenceException" in...
0
by: sarah | last post by:
Using .NET C# installed Framework 1.1 with Service Pack 1 I have a MDI application in C# that contains 3 different forms. Form 1 has several controls one of which is a DataGrid control that...
2
by: Shannon | last post by:
I am having a very strange issue with my asp.net application. I have created a datagrid which dynamically gets populated from a person choosing a value from a dropdown list and entering a quantity...
0
by: muralidharan | last post by:
WebForm1.aspx Code: <%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %> <ComponentArt:TreeView id="TreeView1" Height="520"...
3
by: Patrick.O.Ige | last post by:
I'm loading an Array below but getting the error "Object reference not set to an instance saying 'ItemNumber = CType(Args.Item.FindControl("ItemNumber"), TextBox).Text' is the error line. I DON'T...
5
by: Patrick.O.Ige | last post by:
What could cause the error:- System.NullReferenceException: Object reference not set to an instance of an object. Any ideas?
3
by: postings | last post by:
Hi Can anybody help? (ASP.NET 1.1) The event below is triggered when the user clicks an update button column. I am trying to update a dataset which eventually gets bound with a datagrid,...
3
by: mahtan | last post by:
Please help I have the problem that when I change the data in a Windows.Forms.DataGrid by a separate thread the following Exception is thrown: ThreadSystem.NullReferenceException in...
2
by: Lars Netzel | last post by:
Hi What events are there to do things in when I am on a row in a datagrid (bound from a dataset) and press Delete key... There's no Delete Event on the drig and I can't figure out if there are...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.