2012-01-28 67 views
1

我似乎無法綁定到TextBox。下面是我的源:無法將TextBox綁定到BindingSource

public partial class formAirFreightLabels : Form 
{ 
    int pfDeclarationUID = -1; 
    BindingNavigator bindingNavigatorMain = new BindingNavigator(); 
    BindingSource bindingSourceMain = new BindingSource(); 

    public formAirFreightLabels(int pvDeclarationUID) 
    { 
     InitializeComponent(); 
     pfDeclarationUID = pvDeclarationUID; 

     this.bindingNavigatorMain.BindingSource = this.bindingSourceMain; 
     this.bindingNavigatorMain.Dock = DockStyle.Bottom; 
     this.Controls.Add(this.bindingNavigatorMain); 

     this.Load += new EventHandler(formAirFreightLabels_Load); 
    } 

    void formAirFreightLabels_Load(object sender, EventArgs e) 
    { 
     SqlConnection cn = Program.GetSQLConnection(); 
     if (cn != null) 
     { 
      SqlCommand cmd = new SqlCommand(String.Format("SELECT ShipperName, ShipperAddress, ConsigneeName, ConsigneeAddress FROM Declarations WHERE DeclarationUID={0}", pfDeclarationUID), cn); 
      SqlDataReader r = cmd.ExecuteReader(CommandBehavior.CloseConnection); 
      DataSet ds = new DataSet("Declarations"); 
      ds.Load(r, LoadOption.OverwriteChanges, new string[] { "Declarations" }); 
      bindingSourceMain.DataSource = ds; 
      textBoxShipperName.DataBindings.Add(new Binding("Text", bindingSourceMain, "ShipperName", true)); 
     } 
    } 
} 

我不斷收到一個運行時錯誤如下:

不能綁定到數據源的屬性或列ShipperName。 參數名稱:dataMember

回答

5

正如msdn所說,您需要添加TableName.ColumnName以綁定控件。 Windows Form Binding,你可以嘗試這樣。

textBoxShipperName.DataBindings.Add(new Binding("Text", bindingSourceMain, "Declarations.ShipperName", true)) 
+0

謝謝。我從MSDN得到了這個例子。微軟應該更新他們的例子。 – user1175097 2012-01-28 18:10:49

+3

:P,如果這有助於接受答案 – 2012-01-28 18:13:30