2017-02-24 12 views
0

説明が少し複雑です。だから私は下のいくつかの写真で説明することができます。まず第一に、私はコンボボックスロードされたとのウィンドウが、私はすべてのものが働いているようにVB.NET 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 

としてコンボボックスにデータテーブルをバインド次に、このような

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に私のコンボボックスをバインドここに。ですから、コンボボックスから項目を選択すると、メッセージボックスがこのように表示されます 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ここ ための背後にあるコードで追加した新製品のコードです。それは新しいレコードがデータベースに保存されるまで、新しいレコードが、これは正しいだろう、ゼロのIDが割り当てられますようであれば

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でしょうか?もしそうなら、このコードがヒットします。

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

グーグルでは、 。しかし、どうすればそれを修正できますか? – Student

+0

SelectedValueを使用する代わりにSelectedItemを使用してdatarowにキャストすると、選択したItemのすべてのプロパティを取得できます。何かのようなIF Product_Combobox.SelectedItem ISNOT NULL次にDim RowをDataRow = DirectCast(Product_Combobox.SelectedItem、DataRow) –

関連する問題