2012-01-28 26 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からその例を得ました。 Microsoftは例を更新する必要があります。 – user1175097

+3

:これが答えを受け入れるのに役立つならば、P –

関連する問題