可以';t将TextBox绑定到BindingSource

本文关键字:TextBox 绑定 BindingSource 可以 | 更新日期: 2023-09-27 18:21:56

我似乎无法绑定到TextBox。这是我的来源:

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));
        }
    }
}

我一直得到一个运行时错误如下:

无法绑定到DataSource上的属性或列ShipperName。参数名称:dataMember

可以';t将TextBox绑定到BindingSource

正如msdn所说,您需要添加TableName.ColumnName才能绑定控件。Windows窗体绑定,您可以这样尝试。

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