Click here to Skip to main content
15,921,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Comments
Sergey Alexandrovich Kryukov 20-Nov-12 14:59pm    
System.Windows.Forms? Tag "Forms".
--SA

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:
C#
string stock_code =dataGridView1.CurrentRow.Cells[0].Value.ToString();
string q = dataGridView1.CurrentRow.Cells[0].Value.ToString();
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Nov-12 15:00pm    
As simple as that, a 5.
--SA
The error means that one of the objects that you use is null.
Try to find out with the debugger which object is null.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900