简体   繁体   English

你调用的对象是空的。 错误

[英]Object reference not set to an instance of an object. Error

I have a datagridview of my Vendor table and then I also have textboxes of all the fields. 我有我的Vendor表的datagridview,然后还有所有字段的文本框。 I have a setVendor method that sets all the textboxes to the values from the datagrid and then I have a currentcellchanged event that sets the textboxes to the right vendor that is clicked on. 我有一个setVendor方法,该方法将所有文本框设置为数据网格中的值,然后有一个currentcellchanged事件,该事件将文本框设置为单击的正确供应商。 Here is my code: 这是我的代码:

private void dataGridVendors_CurrentCellChanged_1(object sender, EventArgs e)
{
    setVendorInfo((Vendor)allTheVendors[this.dataGridVendors.CurrentRow.Index]);
}

//method to set the textboxes with the vendor information
private void setVendorInfo(Vendor aVendor)
{
    //set all the textboxes
    this.txtVendorId.Text = aVendor.VendorId;
    this.txtName.Text = aVendor.Name;
    this.txtAddressNo.Text = aVendor.AddressNo;
    this.txtStreet.Text = aVendor.Address;
    this.txtCity.Text = aVendor.City;
    this.comboBoxState.Text = aVendor.State;
    this.txtZipcode.Text = aVendor.Zipcode;
    this.txtPhoneNumber.Text = aVendor.PhoneNumber;
}

The error occurs when I delete a record, and happens in the dataGridVendors_CurrentCellChanged event. 当我删除记录时发生错误,并且发生在dataGridVendors_CurrentCellChanged事件中。 I'm assuming it happens because once the record that is selected is deleted, then there is no record selected, so it throws the error, but I'm not sure how to fix it. 我假设发生这种情况是因为一旦删除了选定的记录,便没有选定的记录,因此它会引发错误,但是我不确定如何解决。

I did notice that if I use a dataGrid, everything works fine, but when I switch it to a dataGridView this error happens. 我确实注意到,如果我使用dataGrid,则一切正常,但是当我将其切换到dataGridView时,会发生此错误。 I would like to use a dataGridView though because I think it looks a little better and I like the autosize columns feature. 我想使用dataGridView,因为我认为它看起来更好一些,并且我喜欢自动调整列大小功能。

Before access to the current row test if it is null 在访问当前行之前测试是否为空

if(this.dataGridVendors.CurrentRow != null )
    setVendorInfo((Vendor)allTheVendors[this.dataGridVendors.CurrentRow.Index]); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误“对象引用未设置为对象的实例。” - Error 'Object reference not set to an instance of an object.' 你调用的对象是空的。 错误 - Object reference not set to an instance of an object. Error 错误“未将对象引用设置为对象的实例。” 对于 CreateUserWithEmailAndPasswordAsync - Error "Object reference not set to an instance of an object." for CreateUserWithEmailAndPasswordAsync 你调用的对象是空的。 请协助解决错误 - Object reference not set to an instance of an object. Please Help to solve error SqlConnection出现错误“对象引用未设置为对象的实例。” - Error “Object reference not set to an instance of an object.” for SqlConnection 没有行号和类别的错误:“对象引用未设置为对象的实例。” - Error with no line number and class: “Object reference not set to an instance of an object.” 表读取错误:“对象引用未设置为对象的实例。” - error in table reading :“Object reference not set to an instance of an object.” C#,“对象引用未设置为对象的实例。”错误 - C#, “Object reference not set to an instance of an object.” error 在mvc项目中插入错误:{“对象引用未设置为对象的实例。”} - Insertion in mvc project error: {“Object reference not set to an instance of an object.”} 单元测试错误“对象引用未设置为对象的实例。” - Unit Testing error “Object reference not set to an instance of an object.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM