2014-05-17 101 views
-1

我有一個問題:我必須形式。一個表單有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(); 
    } 
} 
+0

上線你得到異常?請解釋。 – Pawan

回答

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

0

對於Form2訪問Form1公共成員,你可以在構造相同的情況下發送到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 
} 
相關問題