473,320 Members | 2,133 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.

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 5004
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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: 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: 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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.