Es ist ein wenig kompliziert zu erklären. Lassen Sie mich das mit ein paar Bildern erklären. Zunächst einmal binde ich meine Combobox meiner Datentabelle wieVB.NET Combobox zeigt den Datensatznamen, aber keine Daten?
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
Als nächstes, wenn das Fenster mit der Combobox geladen, ich die Datentabelle auf der Combobox wie
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
Jedes Ding arbeitet binden Hier. Also, wann immer ich einen Artikel aus der Combobox auswähle, wird eine Meldung wie folgt angezeigt:
Also hier ist, wo das Problem beginnt. Wann immer ich ein neues Produkt wie unten hinzufügen, Und klicken Sie auf das neu hinzugefügte Produkt, scheinen die Daten fehlen? Aber die Combobox zeigt immer noch den neuen Produkttext. Die Nachrichtenbox, die zeigen sollte, welche ID ausgewählt ist, erscheint nicht einmal. Die Messagebox zeigt alle anderen Produkte an, aber nicht das neu hinzugefügte Produkt.
Ich entschuldige mich für die lange Frage, wie ich mein Bestes versuche, es richtig zu erklären. Hier ist der Code hinter der Combobox
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 Hier ist der Code für das neue Produkt hinzuzufügen.
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
Wie ist der Event-Handler für die Schaltfläche Bestätigen, wo ich denke, dass Sie das Element hinzufügen, implementiert? – mm8
@ mm8 Meine Frage wurde mit dem neuen Produktcode aktualisiert. – Student
Sie sollten die ID des neu hinzugefügten Produkt eingestellt: newProduct.Item („ID“) = 100. – mm8