【问题标题】:BindingList becomes null after getting data from BindingSource从 BindingSource 获取数据后 BindingList 变为 null
【发布时间】:2013-01-29 05:18:32
【问题描述】:
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<BugClass> bindingList = new BindingList<BugClass>();

一旦出现以下行,它就会变为 null:

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

提前感谢您的帮助

【问题讨论】:

    标签: c# bindingsource bindinglist


    【解决方案1】:

    实际上,它被一次又一次地初始化和销毁​​,foreach (DataGridViewRow item in this.dataGridView1.SelectedRows) ànd 每次button_ChangeStatus_Click 触发。 这就是对象引用未设置为对象实例的地方。来自。

    在别处声明,例如包含类的字段或属性在顶部。这样,它在任何地方都可用,并且可以在事件处理程序中进行分配。

    声明(顶部,带有其他字段/属性):

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

    初始化(在构造函数中):

    bindingList = new BindingList<BugClass>();
    

    分配/更新:

    随心所欲。

    【讨论】:

      猜你喜欢
      • 2021-09-15
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多