0
private void button_ChangeStatus_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
    {
        BindingList<BugClass> bindingList = new BindingList<BugClass>();
        bindingList = this.bindingSource.DataSource as BindingList<BugClass>;

        bindingList[item.Index].Status = txtBox_StatusChange.Text;
    }
}

i keep getting "Object reference not set to an instance of an object." I know this is because it's not initialized, however, it is initialized when here, showing that there is a an empty class:

BindingList<BugClass> bindingList = new BindingList<BugClass>();

then it becomes null as soon as the following line occurs:

bindingList = this.bindingSource.DataSource as BindingList<BugClass>;

Thanks for the help in advance

Alexander
  • 2,320
  • 2
  • 25
  • 33
user1930824
  • 655
  • 1
  • 5
  • 6

1 Answers1

0

Actually, it's being initialized ànd destroyed time and again, foreach (DataGridViewRow item in this.dataGridView1.SelectedRows) ànd every time button_ChangeStatus_Click fires. That's where the Object reference not set to an instance of an object. comes from.

Declare it elsewhere, e.g. with the fields or properties of the containing class up top. That way, it's available everywhere and the assignment can happen inside the event handler.

Declaration (op top, with the other fields / properties):

private BindingList<BugClass> bindingList { get; set; }

Initialization (in the constructor):

bindingList = new BindingList<BugClass>();

Assignment / update:

wherever you want.

Wim Ombelets
  • 5,097
  • 3
  • 39
  • 55