Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Hello,
 
How to get a value from selected row and cell from datagridview before deleting it?
 
This is my code
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
                string stock_code =dataGridView1.CurrentRow.Cells[0].Value.ToString();
                string q = dataGridView1.CurrentRow.Cells[0].Value.ToString();
 
gives this error:
Object reference not set to an instance of an object
Posted 20-Nov-12 7:08am
Comments
Sergey Alexandrovich Kryukov at 20-Nov-12 14:59pm
   
System.Windows.Forms? Tag "Forms".
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Right now your code is reversed. You delete the row and then try to read its values. Put the delete last and if you coded it right, you should get the information you want. Here is what it should look like:
string stock_code =dataGridView1.CurrentRow.Cells[0].Value.ToString();
string q = dataGridView1.CurrentRow.Cells[0].Value.ToString();
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 20-Nov-12 15:00pm
   
As simple as that, a 5.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The error means that one of the objects that you use is null.
Try to find out with the debugger which object is null.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100