2012-02-19 113 views
2

嗨,大家好,我有這個代碼在我ViewProductsInventory(這是從我MainForm通過ShowDialog叫):觸發事件

Private Sub ViewProductsInventory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

    Me.Tb_inventory_datesTableAdapter.Fill(Me.InventorySysDataSet.tb_inventory_dates) 
    Dim inventory_date As Date 
    inventory_date = Me.cboInventoryDate.Text 

End Sub 


Public Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click 
    Dim inventory_date As String 
    inventory_date = Me.cboInventoryDate.Text 

    'this part populates my datagridview1 
    Me.SP_GetInventoryTableAdapter.Fill(Me.InventorySysDataSet.SP_GetInventory, inventory_date) 
End Sub 

在點擊editbutton將執行EditForm.ShowDialog()

,並在我的EditForm該記錄將被更新,之後將觸發ViewProductsInventory.btnSearch_Click(ViewProductsInventory.btnSearch, EventArgs.Empty)

然後出現「從字符串轉換」錯誤到類型「日期」無效的錯誤。

我試圖Msgbox(Me.cboInventoryDate.Text),並且沒有返回。我假設我的cboInventoryDate目前沒有被填充,因此當btnSearch_Click被觸發時它什麼也沒收到。

我該如何解決這個問題?請好心協助我。 TIA!

+0

哪裏cboInventoryDate位於何處? MainForm,ViewProductsInventory或EditForm。你如何填充它? – 2012-02-19 05:46:39

+0

。它位於ViewProductsInventory中。它由此填充> Me.Tb_inventory_datesTableAdapter.Fill(Me.InventorySysDataSet.tb_inventory_dates) – zerey 2012-02-19 05:50:36

+0

有多少條記錄被返回到您的ComboBox,您如何選擇您想要的? – 2012-02-19 06:08:32

回答

2

嘗試這樣的事情在你的btnSearch_Click事件:

If Me.cboInventoryDate.Items.Count > 0 then 
    Me.cboInventoryDate.SelectedIndex = 0 
    inventory_date = Me.cboInventoryDate.Text 

    'this part populates my datagridview1 
    Me.SP_GetInventoryTableAdapter.Fill(Me.InventorySysDataSet.SP_GetInventory, inventory_date) 

End If  
+0

。這將無法正常工作。這將影響按鈕的自然行爲。假設您單擊按鈕搜索特定日期的記錄,Me.cboInventoryDate.SelectedIndex = 0行將觸發對cboInventoryDate的選擇更改,從而禁用您選擇不同的日期。 – zerey 2012-02-19 06:55:19

+0

是的你是對的。嘗試使用Items.Count方法來確定控件是否有數據。您可能需要在程序中創建另一個方法,然後從EditForm中調用它,然後等待ComboBox被填充後再調用Click事件。 – 2012-02-19 08:05:54