2017-02-24 31 views
0

解释有点复杂。所以请允许我用下面的几张图解释。首先,我在我的组合框绑定到我的数据表,如VB.NET Combobox显示记录名称但没有数据?

Try 
     Using myConn As New MySqlConnection(connStr) 
      myCommand = New MySqlCommand("SELECT * FROM product_list", myConn) 
      productDT = New DataTable() 
      productDA = New MySqlDataAdapter(myCommand) 
      Dim myCB As New MySqlCommandBuilder(productDA) 
      productDA.SelectCommand = myCommand 
      productDA.InsertCommand = myCB.GetInsertCommand 
      productDA.UpdateCommand = myCB.GetUpdateCommand 
      productDA.DeleteCommand = myCB.GetDeleteCommand 

      productDA.Fill(productDT) 
     End Using 
    Catch ex As MySqlException 
     WriteExceptionErrorToFile("Main Page.xaml", "Window_Loaded()", ex.ToString()) 
     MsgBox("Please make sure that your database server is ONLINE.", MsgBoxStyle.Critical) 
     Me.Close() 
    End Try 

接下来,当加载组合框的窗口,我将DataTable绑定到ComboBox如

Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) 
    Product_Combobox.ItemsSource = Nothing 
    Product_Combobox.ItemsSource = productDT.DefaultView 
    Product_Combobox.DisplayMemberPath = "Product" 'This is the table's column name 
    Product_Combobox.SelectedValuePath = "ID" 'This one as well. (For ID) 
End Sub 

每一件事情是工作这里。所以每当我从组合框中选择一个项目,一个消息框就会出现像这样 This is when I selected the product 8

所以这里是问题的出发点。每当我添加如下新产品时, enter image description here 然后点击新添加的产品,数据似乎丢失了?但组合框仍然显示新的产品文本。假设显示选择ID的消息框甚至没有弹出。 该消息框显示所有其他产品,但不包括添加的新产品enter image description here

对于长期的问题,我很抱歉,因为我正在尽我所能来正确解释它。 这里是后面的下拉框

Private Sub Product_Combobox_DropDownClosed(sender As Object, e As EventArgs) Handles Product_Combobox.DropDownClosed 
    If Product_Combobox.Text.Trim().Length() < 1 Or Product_Combobox.SelectedValue < 1 Then 
     Exit Sub 
    End If 
    whichID = New Integer 
    whichID = Integer.Parse(Product_Combobox.SelectedValue) 
    MsgBox(whichID.ToString()) 
    Try 

     For Each checkRow As DataRow In productDT.Rows() 
      If Not IsDBNull(checkRow("ID")) Then 
       If checkRow("ID") = whichID Then 
        Amend_Customer_Price_Input.Text = checkRow("Customer Price") 
        Amend_Agent_Price_Input.Text = checkRow("Agent Price") 
        Amend_Unit_Price_Input.Text = checkRow("Unit Price") 
        Amend_Quantity_Input.Text = checkRow("Quantity") 
       End If 
      End If 
     Next 
    Catch ex As MySqlException 
     WriteExceptionErrorToFile("Product Page.xaml", "Product_Combobox_DropDownClosed()", ex.ToString()) 
     MsgBox("Error Code : " + ex.Number().ToString() + " - " + ex.Message + vbNewLine + vbNewLine + "An error log file, AMErrLog has been generated on your desktop. Please forward it to : [email protected]", MsgBoxStyle.Critical) 
    End Try 
    Product_Changes_Input.Text = Product_Combobox.Text.Trim() 
End Sub 

UPDATE 下面的代码是为增加新的产品代码。

Private Sub New_Product_Confirm_Btn_Click(sender As Object, e As RoutedEventArgs) Handles New_Product_Confirm_Btn.Click 
    Dim result As MsgBoxResult = MsgBox("Confirm?", MsgBoxStyle.YesNo) 
    If result = MsgBoxResult.Yes Then 
     If Product_Name_Input.Text.Trim().Length() < 1 Then 
      MsgBox("Product Name is empty!", MsgBoxStyle.Information) 
      Exit Sub 
     End If 
    Else 
     Exit Sub 
    End If 

    Try 
     Dim newProduct = productDT.NewRow() 
     newProduct.Item("Product") = Product_Name_Input.Text.Trim() 
     newProduct.Item("Customer Price") = Decimal.Parse(Customer_Price_Input.Text.Trim()) 
     newProduct.Item("Agent Price") = Decimal.Parse(Agent_Price_Input.Text.Trim()) 
     newProduct.Item("Unit Price") = Decimal.Parse(Unit_Price_Input.Text.Trim()) 
     newProduct.Item("Quantity") = Integer.Parse(Quantity_Input.Text.Trim()) 
     productDT.Rows.Add(newProduct) 

     productDA.Update(productDT) 
     MsgBox("Successfully added.", MsgBoxStyle.Information) 
     Product_Combobox.SelectedIndex = -1 
     Product_Changes_Input.Clear() 
     Amend_Customer_Price_Input.Text = "0.00" 
     Amend_Agent_Price_Input.Text = "0.00" 
     Amend_Unit_Price_Input.Text = "0.00" 
     Amend_Quantity_Input.Text = "0" 
    Catch ex As MySqlException 
     WriteExceptionErrorToFile("Product Page.xaml", "New_Product_Confirm_Btn_Click()", ex.ToString()) 
    End Try 
End Sub 
+0

如何确认按钮的事件处理程序,我猜你添加了该项目,实现了? – mm8

+0

@ mm8用添加新产品代码更新了我的问题。 – Student

+0

您应该设置新添加的产品的ID:newProduct.Item(“Id”)= 100. – mm8

回答

0

Product_Combobox.SelectedValue将< 1,如果它是一个新的记录作为新记录将分配为零的ID,直到保存到数据库中,这会是正确的吗?如果是这样,这段代码将被击中。

If Product_Combobox.Text.Trim().Length() < 1 Or Product_Combobox.SelectedValue < 1 Then 
     Exit Sub 
End If 
+0

随着一些使用Google搜索,我知道它的原因。但是,我该如何解决它? – Student

+0

而不是使用SelectedValue,使用SelectedItem并将其转换为数据行,然后您可以获取所选项目的所有属性。类似于IF Product_Combobox.SelectedItem ISNOT NULL然后将行变暗为DataRow = DirectCast(Product_Combobox.SelectedItem,DataRow) –

相关问题