2012-04-29 16 views
0

私は、ベンダーテーブルのdatagridviewを持っているし、すべてのフィールドのテキストボックスも持っています。私は、すべてのテキストボックスをデータグリッドからの値に設定し、次にクリックされた正しいベンダーにテキストボックスを設定するcurrentcellchangedイベントを持つsetVendorメソッドを持っています。ここに私のコードは次のとおりです。オブジェクト参照がオブジェクトのインスタンスに設定されていません。エラー

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_CurrentCellChangedイベントで起こります。選択されたレコードが削除されると、レコードが選択されていないので、エラーがスローされますが、修正方法はわかりません。

dataGridを使用するとすべて正常に動作しますが、dataGridViewに切り替えるとこのエラーが発生します。私はdataGridViewを使用したいと思いますが、それは少し良く見えると思いますし、列サイズを自動サイズ調整するのが好きです。現在の行のテストにアクセスする前に

答えて

2

それは完全に働いた

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

おかげでたくさん、nullの場合! –

関連する問題