C# 对象引用未设置为对象的实例。错误
我有一个供应商表的datagridview,然后还有所有字段的文本框。我有一个setVendor方法,将所有文本框设置为datagrid中的值,然后有一个currentcellchanged事件,将文本框设置为单击的正确供应商。这是我的密码:C# 对象引用未设置为对象的实例。错误,c#,datagridview,C#,Datagridview,我有一个供应商表的datagridview,然后还有所有字段的文本框。我有一个setVendor方法,将所有文本框设置为datagrid中的值,然后有一个currentcellchanged事件,将文本框设置为单击的正确供应商。这是我的密码: private void dataGridVendors_CurrentCellChanged_1(object sender, EventArgs e) { setVendorInfo((Vendor)allTheVendors[this.dat
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;
}
该错误发生在我删除记录时,并且发生在dataGridVendors\u CurrentCellChanged事件中。我假设发生这种情况是因为一旦选中的记录被删除,那么就没有选中的记录,所以它会抛出错误,但我不确定如何修复它
我确实注意到,如果我使用dataGrid,一切正常,但当我将其切换到dataGridView时,就会发生此错误。但是我想使用dataGridView,因为我认为它看起来更好一点,而且我喜欢自动调整列大小的功能。在访问当前行测试之前,如果它为空
if(this.dataGridVendors.CurrentRow != null )
setVendorInfo((Vendor)allTheVendors[this.dataGridVendors.CurrentRow.Index]);
在访问当前行之前,如果当前行为空,则进行测试
if(this.dataGridVendors.CurrentRow != null )
setVendorInfo((Vendor)allTheVendors[this.dataGridVendors.CurrentRow.Index]);