我有一個問題:我必須形式。一個表單有dataGridView和一個按鈕。當您單擊該按鈕時,創建Form2,在其中輸入信息。這些信息需要添加到第一個表單上的dataGridView。但是,當我點擊Form2中的「添加」按鈕, 我有一個錯誤NullReferenceException未處理(對象引用未設置爲對象的實例。)。請幫幫我!從一種窗體發送信息到另一窗體WindowsForms
Form1中
private string client = null;
private string driver = null;
private string carModel = null;
private string carKey=null;
public string GoodsName2 = null;
public string GoodsPrice2 = null;
public string GoodsCount2 = null;
AddWaybilGoods add_waibil_goods = null;
public Waybil()
{
InitializeComponent();
Base base_ = new Base(Share.server_address, Share.login, Share.password, Share.database);
base_.fill_combo(comboClients, "Clients", "ClientsName");
base_.fill_combo(comboDrivers, "Drivers", "DriversName");
base_.fill_combo(comboModel, "Cars", "CarsModel");
}
private void Create_Click(object sender, EventArgs e)
{
client = comboClients.SelectedItem.ToString();
driver = comboDrivers.SelectedItem.ToString();
carModel = comboModel.SelectedItem.ToString();
carKey =Key.Text.ToString();
Word.Application word = new Word.Application();
word.Visible = true;
Word.Document doc = word.Documents.Add();
doc.Select();
word.Selection.TypeText(client);
word.Selection.TypeText(driver);
word.Selection.TypeText(carModel);
word.Selection.TypeText(carKey);
}
private void Add_Click(object sender, EventArgs e)
{
add_waibil_goods = new AddWaybilGoods();
add_waibil_goods.Owner = this;
add_waibil_goods.Show();
add_waibil_goods.Focus();
dataGridView1.Rows.Add(GoodsName2,GoodsPrice2, GoodsCount2);
dataGridView1.Update();
}
private void comboModel_SelectedIndexChanged(object sender, EventArgs e)
{
carModel = comboModel.SelectedItem.ToString();
Base base_ = new Base(Share.server_address, Share.login, Share.password, Share.database);
base_.find_item(Key, "Cars", "CarsKey", "CarsModel", carModel);
}
}
窗體2
public partial class AddWaybilGoods : Form
{
Waybil w_b = null;
public string GoodsName1 = null;
public string GoodsPrice1 = null;
public string GoodsCount1 = null;
public AddWaybilGoods()
{
InitializeComponent();
w_b= this.Owner as Waybil;
Base base_ = new Base(Share.server_address, Share.login, Share.password, Share.database);
base_.fill_combo(AddWaybilGoods1, "Goods", "GoodsName");
}
public void Добавить_Click(object sender, EventArgs e)
{
GoodsCount1 = Count.Text.ToString();
w_b.GoodsName2 = this.GoodsName1;
w_b.GoodsPrice2 = GoodsPrice1;
w_b.GoodsCount2 = GoodsCount1;
this.Close();
}
private void AddWaybilGoods1_SelectedIndexChanged(object sender, EventArgs e)
{
GoodsName1 = AddWaybilGoods1.SelectedItem.ToString();
Base base_ = new Base(Share.server_address, Share.login, Share.password, Share.database);
base_.find_item(Price, "Goods","GoodsPrice", "GoodsName", GoodsName1);
GoodsPrice1 = Price.Text.ToString();
}
}
上線你得到異常?請解釋。 – Pawan