Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. code dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString() doesnt work

code dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString() doesnt work

Scheduled Pinned Locked Moved C#
help
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aahamdan
    wrote on last edited by
    #1

    Dear Experts, I am trying to get the value for current cell in DatagridView. I wrote code

    dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();

    in CellLeave event. but the code raise an error. Object reference not set to an instance of an object. But if I write the code in Button_Click event, it is working properly. Please help. Ahmad

    OriginalGriffO H V 3 Replies Last reply
    0
    • A aahamdan

      Dear Experts, I am trying to get the value for current cell in DatagridView. I wrote code

      dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();

      in CellLeave event. but the code raise an error. Object reference not set to an instance of an object. But if I write the code in Button_Click event, it is working properly. Please help. Ahmad

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Use the debugger. When you get the exception, use the debugger to look at the various parts of that, and work out what the values of each part are: e.ColumnIndex, e.RowIndex, and so forth. Personally, I'd be using dataGridView1.Rows[rowindex].Cells[columnIndex] and checking the return value to make sure there is one. I suspect you have just mixed up the row and column, and been lucky not to get an index out of range error. But the debugger is the best way to find out.

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      A 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Use the debugger. When you get the exception, use the debugger to look at the various parts of that, and work out what the values of each part are: e.ColumnIndex, e.RowIndex, and so forth. Personally, I'd be using dataGridView1.Rows[rowindex].Cells[columnIndex] and checking the return value to make sure there is one. I suspect you have just mixed up the row and column, and been lucky not to get an index out of range error. But the debugger is the best way to find out.

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        A Offline
        A Offline
        aahamdan
        wrote on last edited by
        #3

        Thank you Sir, I have tried your code, but I get the same error. Below is the exception message I received. The code works under Button_Cleck event.

        System.NullReferenceException was unhandled
        Message=Object reference not set to an instance of an object.
        Source=ERP
        StackTrace:
        at ERP.frmCalendar.dataGridView1_CellLeave(Object sender, DataGridViewCellEventArgs e) in C:\Projects\ERP\ERP\GL\frmCalendar.cs:line 103
        at System.Windows.Forms.DataGridView.OnCellLeave(DataGridViewCellEventArgs e)
        at System.Windows.Forms.DataGridView.OnCellLeave(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex)
        at System.Windows.Forms.DataGridView.CommitEdit(DataGridViewCell& dataGridViewCurrentCell, DataGridViewDataErrorContexts context, DataGridViewValidateCellInternal validateCell, Boolean fireCellLeave, Boolean fireCellEnter, Boolean fireRowLeave, Boolean fireRowEnter, Boolean fireLeave)
        at System.Windows.Forms.DataGridView.EndEdit(DataGridViewDataErrorContexts context, DataGridViewValidateCellInternal validateCell, Boolean fireCellLeave, Boolean fireCellEnter, Boolean fireRowLeave, Boolean fireRowEnter, Boolean fireLeave, Boolean keepFocus, Boolean resetCurrentCell, Boolean resetAnchorCell)
        at System.Windows.Forms.DataGridView.CommitEditForOperation(Int32 columnIndex, Int32 rowIndex, Boolean forCurrentCellChange)
        at System.Windows.Forms.DataGridView.ScrollIntoView(Int32 columnIndex, Int32 rowIndex, Boolean forCurrentCellChange)
        at System.Windows.Forms.DataGridView.TabToNextCell()
        at System.Windows.Forms.DataGridView.ProcessTabKey(Keys keyData)
        at System.Windows.Forms.DataGridView.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.Control.PreProcessControlMessageInternal(Control target, Message& msg)
        at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
        at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg)
        at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
        at System.Windows.Forms.Application.Thread

        OriginalGriffO 1 Reply Last reply
        0
        • A aahamdan

          Dear Experts, I am trying to get the value for current cell in DatagridView. I wrote code

          dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();

          in CellLeave event. but the code raise an error. Object reference not set to an instance of an object. But if I write the code in Button_Click event, it is working properly. Please help. Ahmad

          H Offline
          H Offline
          Henrik Jonsson
          wrote on last edited by
          #4

          Hi, when you get "Object reference not set to an instance of an object" exceptions some of the properties you invoke have returned null, in this case most likely Value for an empty grid cell. Check the Value for null before using it:

          var gridValue = dataGridView1[e.ColumnIndex, e.RowIndex].Value;
          if( gridValue != null )
          {
          string gridStringValue = gridValue.ToString();
          ...
          }

          1 Reply Last reply
          0
          • A aahamdan

            Thank you Sir, I have tried your code, but I get the same error. Below is the exception message I received. The code works under Button_Cleck event.

            System.NullReferenceException was unhandled
            Message=Object reference not set to an instance of an object.
            Source=ERP
            StackTrace:
            at ERP.frmCalendar.dataGridView1_CellLeave(Object sender, DataGridViewCellEventArgs e) in C:\Projects\ERP\ERP\GL\frmCalendar.cs:line 103
            at System.Windows.Forms.DataGridView.OnCellLeave(DataGridViewCellEventArgs e)
            at System.Windows.Forms.DataGridView.OnCellLeave(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex)
            at System.Windows.Forms.DataGridView.CommitEdit(DataGridViewCell& dataGridViewCurrentCell, DataGridViewDataErrorContexts context, DataGridViewValidateCellInternal validateCell, Boolean fireCellLeave, Boolean fireCellEnter, Boolean fireRowLeave, Boolean fireRowEnter, Boolean fireLeave)
            at System.Windows.Forms.DataGridView.EndEdit(DataGridViewDataErrorContexts context, DataGridViewValidateCellInternal validateCell, Boolean fireCellLeave, Boolean fireCellEnter, Boolean fireRowLeave, Boolean fireRowEnter, Boolean fireLeave, Boolean keepFocus, Boolean resetCurrentCell, Boolean resetAnchorCell)
            at System.Windows.Forms.DataGridView.CommitEditForOperation(Int32 columnIndex, Int32 rowIndex, Boolean forCurrentCellChange)
            at System.Windows.Forms.DataGridView.ScrollIntoView(Int32 columnIndex, Int32 rowIndex, Boolean forCurrentCellChange)
            at System.Windows.Forms.DataGridView.TabToNextCell()
            at System.Windows.Forms.DataGridView.ProcessTabKey(Keys keyData)
            at System.Windows.Forms.DataGridView.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.Control.PreProcessControlMessageInternal(Control target, Message& msg)
            at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
            at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg)
            at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
            at System.Windows.Forms.Application.Thread

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            So use the debugger and find out which bit is null. Then you can look at why! But I can't do that for you - I have no access to your computer...

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • A aahamdan

              Dear Experts, I am trying to get the value for current cell in DatagridView. I wrote code

              dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();

              in CellLeave event. but the code raise an error. Object reference not set to an instance of an object. But if I write the code in Button_Click event, it is working properly. Please help. Ahmad

              V Offline
              V Offline
              V 0
              wrote on last edited by
              #6

              string cellval = "";
              if(dataGridView1[e.ColumnIndex, e.RowIndex].Value != null){
              cellval = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
              }

              I usually use Convert.ToString(...); in such cases, because it will convert a null to "" automatically. (not useful in all cases)

              V.
              (MQOTD rules and previous solutions)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups