我有一个问题:我必须表格。一个Form有dataGridView和一个按钮。单击按钮,创建Form2,您在其中输入信息。并且这些信息需要在First Form上添加到dataGridView。但当我点击按钮"添加"在Form2中, 我有一个错误NullReferenceException未处理(对象引用未设置为对象的实例。)。请帮帮我!
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();
}
}
答案 0 :(得分:0)
更新以下语句应解决您的问题
private void Add_Click(object sender, EventArgs e)
{
add_waibil_goods = new AddWaybilGoods();
if (add_waibil_goods.ShowDialog(this) == DialogResult.OK)
{
dataGridView1.Rows.Add(add_waibil_goods.GoodsName2, add_waibil_goods.GoodsPrice2, add_waibil_goods.GoodsCount2);
dataGridView1.Update();
}
}
在添加到dataGridView1时,您似乎在引用Form1中的属性。您还需要将form2作为模态窗口启动。模态窗口将确保用户在应用程序继续之前填写正确的数据。
http://msdn.microsoft.com/en-us/library/c7ykbedk(v=vs.110).aspx
答案 1 :(得分:0)
要访问Form1
中的Form2
个公开成员,您可以将相同的实例发送到构造函数中的Form1。
请参阅以下代码:
<强> Form1中强>:
private void Add_Click(object sender, EventArgs e)
{
add_waibil_goods = new AddWaybilGoods(this); //parameterized constructor
add_waibil_goods.Show(this); //assign owner
add_waibil_goods.Focus();
dataGridView1.Rows.Add(GoodsName2,GoodsPrice2, GoodsCount2);
dataGridView1.Update();
}
<强>窗体2 强>:
public partial class AddWaybilGoods : Form
{
Waybil w_b = null;
public string GoodsName1 = null;
public string GoodsPrice1 = null;
public string GoodsCount1 = null;
public AddWaybilGoods(Waybil wb)
{
InitializeComponent();
w_b= wb;
Base base_ = new Base(Share.server_address, Share.login, Share.password, Share.database);
base_.fill_combo(AddWaybilGoods1, "Goods", "GoodsName");
}
//... other methods
}