2015-11-04 195 views
0

我一直堅持這個問題幾天,所以我決定創建一個簡單的獨立測試項目來演示我的問題。組合框在創建新記錄後無法正確顯示

我用Visual Studio 2008與C#創建一個Windows窗體應用程序。 我的數據庫是SQL Express。我有一個組合框綁定到只有兩列ID和名稱的表。

我的測試應用程序將清潔劑匹配到建築物。 每座建築可以有一個清潔劑一個清潔劑可以有許多建築物。

清潔表有兩列 CleanerID CleanerName

建表有三列 BuildingID BuildingName BuildingCleanerID

如果我手動用數據填充它所有的作品表。 但是,如果不存在,我希望能夠創建新的建築物,然後從現有記錄中選擇一個清潔工。問題是,創建新記錄時,先前顯示的清理程序仍處於下拉菜單中,而不是剛剛創建的新記錄的值,即Id 0中的第一條記錄,我將其命名爲「Select One」。我已經嘗試了cleanerNameComboBox.SelectedIndex = 0;在代碼中的不同位置,但它似乎沒有任何區別。

我已經使用GUI來連接組合框的所有屬性。 以下是主窗體中的代碼。 如果有人想要測試自己是否有辦法發佈我的測試項目,請告訴我。我可能在這裏錯過了一些簡單的東西,但這讓我感到非常緊張。

在此先感謝大衛。

Screen Shot enter image description here

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace ComboBoxTest 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 


     private void Form1_Load(object sender, EventArgs e) 
     { 
      //This line of code loads data into the 'testComboDataSet.Cleaner' table. You can move, or remove it, as needed. 
      this.cleanerTableAdapter.Fill(this.testComboDataSet.Cleaner); 

     } 

     private void buttonSearch_Click(object sender, EventArgs e) 
     { 
      int rowsFound = this.buildingTableAdapter.FillBuildingByBuildingID(this.testComboDataSet.Building,Convert.ToInt32(buildingIDTextBox.Text)); 
      if (rowsFound == 0) 
      { 
       MessageBox.Show("Building Not Found, Create new record","Info"); 
       TestComboDataSet.BuildingRow newbuildingRow; 
       newbuildingRow = testComboDataSet.Building.NewBuildingRow(); 

       try 
       { 
        // Set the initial values 
        newbuildingRow.BuildingCleanerID = 0; 
        newbuildingRow.BuildingName = "Not Set"; 

        // Add the row to the building table 
        this.testComboDataSet.Building.Rows.Add(newbuildingRow); 

        // Save the Building table row to the database 
        this.buildingTableAdapter.Update(this.testComboDataSet.Building); 

        cleanerNameComboBox.SelectedIndex = 0; 
        // 
        MessageBox.Show("New Building Created", "Info"); 
       } 
       catch (System.Exception er) 
       { 
        MessageBox.Show("Unable to create new building\n" + er, "Error"); 
       } 

      } 

     } 

     private void buttonSave_Click(object sender, EventArgs e) 
     { 
      this.Validate(); 
      this.buildingBindingSource.EndEdit(); 
      this.tableAdapterManager.UpdateAll(this.testComboDataSet); 
     } 
    } 
} 

UPDATE 東西正在更新清潔工表的細節。 不知道這是如何。我只寫信給建築能。 我有組合框的數據綁定完全錯誤嗎?

清潔工表開始作爲

0中選擇一個

1吸塵器的

2清潔乙

3清潔Ç

4清潔d

5清潔劑X

在我的程序中,我將A樓從清潔A改爲了清潔X. 現在清潔工表格看起來就像這樣。 Contents of the Cleaners Table

回答

0

正如我所料,答案很簡單。

真正的問題是,我已經從DataSources選項卡中將CleanerName字段(作爲組合框)拖到我的表單上。我應該從DataSources選項卡拖動整個清潔表(作爲組合框)到我的表單上。這只是讓我從Building表中將控件屬性中的「Selected Value」設置爲BuildingCleanerID。

可悲的是,它在設計器中看起來完全相同,但現在它的工作原理與您預期的完全相同,代碼也很簡單。

此外,爲了避免意外更新Cleaners表,我需要將Combobox下拉樣式從「DropDown」更改爲「DropDownList」,這會阻止用戶將新東西輸入到下拉框中。

像往常一樣,我希望這可以幫助別人有同樣的問題 大衛

相關問題