0

Good evening guyz ,

I use this code to run throw a datagridview and on a specific column reduce the length of the data in the cells. Although the code does its work I get a null exception :

Object reference not set to an instance of an object.

The code that I use :

 string ChangeNumber = dataGridView3.Rows[counter].Cells["Value"].Value.ToString();
 ChangeNumber = ChangeNumber.Remove(ChangeNumber.Length - 2);
 dataGridView3.Rows[counter].Cells["Value"].Value = ChangeNumber;

I also tried to use !string.IsNullOrEmpty but it still didnt work and I got the same exception . Weird thing is that it really does what it's supposed to do but gives me the exception anyways . Any ideas ?

The general Idea might be interpreted in other users quastion but this specific datagridview exception is not analyzed ... I think . Anyway ... GOT it ...

THIS WORKS !

for (counter = 0; counter < (dataGridView3.Rows.Count); counter++)
                    {
                        if (dataGridView3.Rows[counter].Cells["Value"].Value != null)
                        {
                            string ChangeNumber = dataGridView3.Rows[counter].Cells["Value"].Value.ToString();
                            ChangeNumber = ChangeNumber.Remove(ChangeNumber.Length - 2);
                            dataGridView3.Rows[counter].Cells["Value"].Value = ChangeNumber;
                        }

                    }

0 Answers0