0

I have made a Windows forms application using c# and I am trying to add the edit and delete options in my datagridview. I have written the code for reordering of the columns using DisplayIndex as shown below but it throws a NullReferenceException and gives an error 'Object reference not set to an instance of an object'.I have included these statements in the FormName_Load method and I am using Visual Studio Professional 2013.

        DataGridViewLinkColumn EditLink = new DataGridViewLinkColumn();
        EditLink.UseColumnTextForLinkValue = true;
        EditLink.HeaderText = "Edit";
        //EditLink.DataPropertyName = dataGridView1.ToString();
        EditLink.LinkBehavior = LinkBehavior.SystemDefault;
        EditLink.Text = "edit";
        dataGridView1.Columns.Add(EditLink);
        dataGridView1.Columns["Edit"].DisplayIndex = 9;
        //dataGridView1.Columns["Edit"].Visible = true;
  • 2
    See following link: [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it?rq=1) – bassfader Jul 14 '16 at 11:55

1 Answers1

0

According to MSDN, the DisplayIndex property is zero based, and corresponds to the current position of the column as displayed by the user interface.

Are you certain that

dataGridView1.Columns["Edit"].DisplayIndex = 9;

is in the range of valid indexes?

Paul Roub
  • 35,100
  • 27
  • 72
  • 83
antydsr
  • 16
  • 1