无法将 TextBox 绑定到 BindingSource

发布于 2024-12-29 22:13:13 字数 1485 浏览 1 评论 0原文

我似乎无法绑定到文本框。这是我的来源:

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

I cannot seem to bind to a TextBox. Here is my source:

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

I keep getting a run-time error as follows:

Cannot bind to the property or column ShipperName on the DataSource.
Parameter name: dataMember

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霊感 2025-01-05 22:13:13

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

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

As msdn says, you need to add TableName.ColumnName in order bind the controls. Windows Form Binding , you may try like this.

textBoxShipperName.DataBindings.Add(new Binding("Text", bindingSourceMain, "Declarations.ShipperName", true))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文