Wpf VB.NET Combobox显示的是记录名,但没有数据?

Wpf VB.NET Combobox显示的是记录名,但没有数据?,wpf,vb.net,visual-studio-2015,combobox,datatable,Wpf,Vb.net,Visual Studio 2015,Combobox,Datatable,解释起来有点复杂。请允许我用下面的几张图片来解释。首先,我将combobox绑定到datatable,例如 Try Using myConn As New MySqlConnection(connStr) myCommand = New MySqlCommand("SELECT * FROM product_list", myConn) productDT = New DataTable() produ

解释起来有点复杂。请允许我用下面的几张图片来解释。首先,我将combobox绑定到datatable,例如

   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
接下来,当加载了combobox的窗口时,我将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
这里一切正常。因此,每当我从组合框中选择一个项目时,就会出现一个消息框,如下所示

问题就从这里开始。每当我添加如下新产品时, 并点击新添加的产品,数据似乎丢失了?但是组合框仍然显示新产品文本。原本用来显示所选ID的messagebox甚至没有弹出消息框显示所有其他产品,但不显示新添加的产品

我为这个冗长的问题道歉,因为我正在尽我最大的努力正确地解释它。 下面是组合框的代码

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 : joshlcs93@gmail.com", MsgBoxStyle.Critical)
    End Try
    Product_Changes_Input.Text = Product_Combobox.Text.Trim()
End Sub
Private子产品\u Combobox\u DropDownClosed(发送方作为对象,e作为事件参数)处理产品\u Combobox.DropDownClosed
如果Product\u Combobox.Text.Trim().Length()小于1或Product\u Combobox.SelectedValue小于1,则
出口接头
如果结束
whichID=新整数
whichID=Integer.Parse(产品组合框.SelectedValue)
MsgBox(whichID.ToString())
尝试
对于productDT.Rows()中作为DataRow的每个检查行
如果不是IsDBNull(检查行(“ID”)),则
如果checkRow(“ID”)=那么
修改客户价格输入文本=检查行(“客户价格”)
修改代理价格输入文本=检查行(“代理价格”)
修改\u单价\u Input.Text=checkRow(“单价”)
修改数量输入文本=检查行(“数量”)
如果结束
如果结束
下一个
Catch ex作为MySqlException
WriteExceptionErrorFile(“Product Page.xaml”、“Product\u Combobox\u DropDownClosed()”,例如ToString())
MsgBox(“错误代码:“+ex.Number().ToString()+”-“+ex.Message+vbNewLine+vbNewLine+”桌面上已生成错误日志文件AMErrLog。请将其转发至:joshlcs93@gmail.com,MsgBoxStyle.Critical)
结束尝试
Product\u Changes\u Input.Text=Product\u Combobox.Text.Trim()
端接头
更新 下面是添加新产品的代码

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
Private Sub New\u Product\u Confirm\u Btn\u Click(发送方作为对象,e作为RoutedEventArgs)处理新产品\u Confirm\u Btn。Click
尺寸结果为MsgBoxResult=MsgBox(“确认?”,MsgBoxStyle.YesNo)
如果result=MsgBoxResult.Yes则
如果Product_Name_Input.Text.Trim().Length()小于1,则
MsgBox(“产品名称为空!”,MsgBoxStyle.Information)
出口接头
如果结束
其他的
出口接头
如果结束
尝试
Dim newProduct=productDT.NewRow()
newProduct.Item(“Product”)=Product\u Name\u Input.Text.Trim()
newProduct.Item(“Customer Price”)=Decimal.Parse(Customer\u Price\u Input.Text.Trim())
newProduct.Item(“Agent Price”)=Decimal.Parse(Agent\u Price\u Input.Text.Trim())
newProduct.Item(“单价”)=Decimal.Parse(Unit\u Price\u Input.Text.Trim())
newProduct.Item(“Quantity”)=Integer.Parse(Quantity\u Input.Text.Trim())
productDT.Rows.Add(新产品)
productDA.Update(productDT)
MsgBox(“已成功添加。”,MsgBoxStyle.Information)
Product\u Combobox.SelectedIndex=-1
产品更改输入。清除()
修改客户价格输入。Text=“0.00”
修改\代理\价格\输入。Text=“0.00”
修改单位价格输入。Text=“0.00”
修改数量输入。Text=“0”
Catch ex作为MySqlException
WriteExceptionErrorFile(“Product Page.xaml”、“新产品”确认“点击()”,例如ToString())
结束尝试
端接头

Product\u Combobox.SelectedValue将<1如果它是一条新记录,因为在保存到数据库之前,新记录的ID将为零,这是否正确?如果是这样,这段代码将被命中

If Product_Combobox.Text.Trim().Length() < 1 Or Product_Combobox.SelectedValue < 1 Then
        Exit Sub
End If
如果Product\u Combobox.Text.Trim().Length()小于1或Product\u Combobox.SelectedValue<1,则
出口接头
如果结束

Confirm按钮的事件处理程序是如何实现的?我猜您在该按钮中添加了项目?@mm8也用添加新产品代码更新了我的问题。您应该设置新添加产品的Id:newProduct.item(“Id”)=100。@mm8但在数据库中,Id设置为auto increment Already,并通过一些谷歌搜索,我知道原因。但是如何修复它呢?不要使用SelectedValue,而是使用SelectedItem并将其强制转换为datarow,这样您就可以获得所选项目的所有属性。类似于如果Product\u Combobox.SelectedItem不为NULL,则将行变暗为DataRow=DirectCast(Product\u Combobox.SelectedItem,DataRow)