2013-01-05 63 views
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; 
    } 
} 

我一直在收到「對象引用未設置爲對象的實例」。 我知道這是因爲它沒有初始化,但是, 它被初始化的時候,這裏,可見是有一個空類:BindingList從BindingSource獲取數據後變爲空

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

然後儘快以下行發生就變得空:

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

感謝您的幫助提前

回答

0

事實上,它被初始化和銷燬​​一次又一次,foreach (DataGridViewRow item in this.dataGridView1.SelectedRows),每次button_ChangeStatus_Click火災。 這就是對象引用未設置爲對象的實例的位置。來自。

在其他地方聲明,例如,包含類的字段或屬性位於頂部。這樣,它可以在任何地方使用,並且可以在事件處理程序內部進行分配。

宣言(OP頂部,與其他字段/屬性):

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

初始化(構造函數):

bindingList = new BindingList<BugClass>(); 

分配/更新:

,無論你想。

相關問題