NullReferenceException when delete last row in a datagrid

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ryan Liu

    NullReferenceException when delete last row in a datagrid

    Hi there,

    I got a NullReferenceEx ception 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.NullRefe renceException: 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. DeleteDataGridR ows(Int32 deletedRows)
    at System.Windows. Forms.DataGrid. DeleteRows(Data GridRow[] localGridRows)
    at System.Windows. Forms.DataGrid. ProcessGridKey( KeyEventArgs ke)
    at System.Windows. Forms.DataGrid. ProcessDialogKe y(Keys keyData)
    at System.Windows. Forms.Control.P rocessDialogKey (Keys keyData)
    at System.Windows. Forms.TextBoxBa se.ProcessDialo gKey(Keys keyData)
    at System.Windows. Forms.Control.P reProcessMessag e(Message& msg)
    at
    System.Windows. Forms.ThreadCon text.System.Win dows.Forms.Unsa feNativeMethods +IMsoComponent. FPreTranslateMe ssage(MSG&
    msg)
    at
    System.Windows. Forms.Component Manager.System. Windows.Forms.U nsafeNativeMeth ods+IMsoCompone ntManager.FPush MessageLoop(Int 32
    dwComponentID, Int32 reason, Int32 pvLoopData)
    at System.Windows. Forms.ThreadCon text.RunMessage LoopInner(Int32 reason,
    ApplicationCont ext context)
    at System.Windows. Forms.ThreadCon text.RunMessage Loop(Int32 reason,
    ApplicationCont ext context)
    at System.Windows. Forms.Applicati on.Run(Form mainForm)


  • Brian Delahunty

    #2
    RE: NullReferenceEx ception when delete last row in a datagrid

    Can you post your code please.

    --
    Brian Delahunty
    Ireland



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


    "Ryan Liu" wrote:
    [color=blue]
    > Hi there,
    >
    > I got a NullReferenceEx ception 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.NullRefe renceException: 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. DeleteDataGridR ows(Int32 deletedRows)
    > at System.Windows. Forms.DataGrid. DeleteRows(Data GridRow[] localGridRows)
    > at System.Windows. Forms.DataGrid. ProcessGridKey( KeyEventArgs ke)
    > at System.Windows. Forms.DataGrid. ProcessDialogKe y(Keys keyData)
    > at System.Windows. Forms.Control.P rocessDialogKey (Keys keyData)
    > at System.Windows. Forms.TextBoxBa se.ProcessDialo gKey(Keys keyData)
    > at System.Windows. Forms.Control.P reProcessMessag e(Message& msg)
    > at
    > System.Windows. Forms.ThreadCon text.System.Win dows.Forms.Unsa feNativeMethods +IMsoComponent. FPreTranslateMe ssage(MSG&
    > msg)
    > at
    > System.Windows. Forms.Component Manager.System. Windows.Forms.U nsafeNativeMeth ods+IMsoCompone ntManager.FPush MessageLoop(Int 32
    > dwComponentID, Int32 reason, Int32 pvLoopData)
    > at System.Windows. Forms.ThreadCon text.RunMessage LoopInner(Int32 reason,
    > ApplicationCont ext context)
    > at System.Windows. Forms.ThreadCon text.RunMessage Loop(Int32 reason,
    > ApplicationCont ext context)
    > at System.Windows. Forms.Applicati on.Run(Form mainForm)
    >
    >
    >[/color]

    Comment

    • Ryan Liu

      #3
      Re: NullReferenceEx ception when delete last row in a datagrid

      Hi Brian,

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

      Thanks a lot!
      Ryan


      "Ryan Liu" <rliu@eSpaceSof t.com> дÈëÏûÏ¢ÐÂÎÅ:OG BijChoFHA.2904@ tk2msftngp13.ph x.gbl...[color=blue]
      > 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.. SelfDefineAbnor malEndGridContr ol inherits RefeshDataGerid Control
      > b.. RefeshDataGerid Control use SysUtil to read and upate database.
      > c.. RefeshDataGerid Control 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 MySqlDataAdapte r
      > BuildDbAdapterF orSelfDefindedA bnormalEnd(IDbC onnection conn){} return a
      > adapter to read and write back to db.
      >
      > b.. SelfDefineAbnor malEndGridContr ol implements RefeshDataGerid Control'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
      > selfDefineAbnor malEndGridContr ol1 on it, and when form is load,
      > SelfDefineAbnor malEndGridContr ol 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" <BrianDelahunty @discussions.mi crosoft.com> дÈëÏûÏ¢ÐÂÎÅ:89 4251E6-A2F7-41C5-8787-7080EE23881F@mi crosoft.com...[color=green]
      >> 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:
      >>[color=darkred]
      >>> Hi there,
      >>>
      >>> I got a NullReferenceEx ception 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.NullRefe renceException: 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. DeleteDataGridR ows(Int32
      >>> deletedRows)
      >>> at System.Windows. Forms.DataGrid. DeleteRows(Data GridRow[]
      >>> localGridRows)
      >>> at System.Windows. Forms.DataGrid. ProcessGridKey( KeyEventArgs ke)
      >>> at System.Windows. Forms.DataGrid. ProcessDialogKe y(Keys keyData)
      >>> at System.Windows. Forms.Control.P rocessDialogKey (Keys keyData)
      >>> at System.Windows. Forms.TextBoxBa se.ProcessDialo gKey(Keys keyData)
      >>> at System.Windows. Forms.Control.P reProcessMessag e(Message& msg)
      >>> at
      >>> System.Windows. Forms.ThreadCon text.System.Win dows.Forms.Unsa feNativeMethods +IMsoComponent. FPreTranslateMe ssage(MSG&
      >>> msg)
      >>> at
      >>> System.Windows. Forms.Component Manager.System. Windows.Forms.U nsafeNativeMeth ods+IMsoCompone ntManager.FPush MessageLoop(Int 32
      >>> dwComponentID, Int32 reason, Int32 pvLoopData)
      >>> at System.Windows. Forms.ThreadCon text.RunMessage LoopInner(Int32
      >>> reason,
      >>> ApplicationCont ext context)
      >>> at System.Windows. Forms.ThreadCon text.RunMessage Loop(Int32 reason,
      >>> ApplicationCont ext context)
      >>> at System.Windows. Forms.Applicati on.Run(Form mainForm)
      >>>
      >>>
      >>>[/color][/color]
      >
      >
      >[/color]


      Comment

      • Ryan Liu

        #4
        Re: NullReferenceEx ception when delete last row in a datagrid

        Proiblen solved, all because I add CurrentCellChan ged 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_Curren tCellChanged(ob ject sender, EventArgs e)
        {
        if(this.dataGri d.CurrentRowInd ex != -1) //not empty table
        {
        this.dataGrid.S elect(this.data Grid.CurrentRow Index);
        }
        }


        "Ryan Liu" <rliu@eSpaceSof t.com> дÈëÏûÏ¢ÐÂÎÅ:Oo 6xes5oFHA.1416@ TK2MSFTNGP09.ph x.gbl...[color=blue]
        > Hi Brian,
        >
        > Have you found any problem with my code? Do you need more source code from
        > me?
        >
        > Thanks a lot!
        > Ryan
        >
        >
        > "Ryan Liu" <rliu@eSpaceSof t.com> дÈëÏûÏ¢ÐÂÎÅ:OG BijChoFHA.2904@ tk2msftngp13.ph x.gbl...[color=green]
        >> 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.. SelfDefineAbnor malEndGridContr ol inherits RefeshDataGerid Control
        >> b.. RefeshDataGerid Control use SysUtil to read and upate database.
        >> c.. RefeshDataGerid Control 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 MySqlDataAdapte r
        >> BuildDbAdapterF orSelfDefindedA bnormalEnd(IDbC onnection conn){} return a
        >> adapter to read and write back to db.
        >>
        >> b.. SelfDefineAbnor malEndGridContr ol implements
        >> RefeshDataGerid Control'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
        >> selfDefineAbnor malEndGridContr ol1 on it, and when form is load,
        >> SelfDefineAbnor malEndGridContr ol 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" <BrianDelahunty @discussions.mi crosoft.com> дÈëÏûÏ¢ÐÂÎÅ:89 4251E6-A2F7-41C5-8787-7080EE23881F@mi crosoft.com...[color=darkred]
        >>> 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 NullReferenceEx ception 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.NullRefe renceException: 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. DeleteDataGridR ows(Int32
        >>>> deletedRows)
        >>>> at System.Windows. Forms.DataGrid. DeleteRows(Data GridRow[]
        >>>> localGridRows)
        >>>> at System.Windows. Forms.DataGrid. ProcessGridKey( KeyEventArgs ke)
        >>>> at System.Windows. Forms.DataGrid. ProcessDialogKe y(Keys keyData)
        >>>> at System.Windows. Forms.Control.P rocessDialogKey (Keys keyData)
        >>>> at System.Windows. Forms.TextBoxBa se.ProcessDialo gKey(Keys keyData)
        >>>> at System.Windows. Forms.Control.P reProcessMessag e(Message& msg)
        >>>> at
        >>>> System.Windows. Forms.ThreadCon text.System.Win dows.Forms.Unsa feNativeMethods +IMsoComponent. FPreTranslateMe ssage(MSG&
        >>>> msg)
        >>>> at
        >>>> System.Windows. Forms.Component Manager.System. Windows.Forms.U nsafeNativeMeth ods+IMsoCompone ntManager.FPush MessageLoop(Int 32
        >>>> dwComponentID, Int32 reason, Int32 pvLoopData)
        >>>> at System.Windows. Forms.ThreadCon text.RunMessage LoopInner(Int32
        >>>> reason,
        >>>> ApplicationCont ext context)
        >>>> at System.Windows. Forms.ThreadCon text.RunMessage Loop(Int32 reason,
        >>>> ApplicationCont ext context)
        >>>> at System.Windows. Forms.Applicati on.Run(Form mainForm)
        >>>>
        >>>>
        >>>>[/color]
        >>
        >>
        >>[/color]
        >
        >[/color]


        Comment

        Working...