Не удается привязать TextBox к BindingSource - PullRequest
1 голос
/ 28 января 2012

Я не могу привязаться к 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));
        }
    }
}

Я получаю ошибку во время выполнения следующим образом:

Невозможно привязать свойство или столбец ShipperName в DataSource.Имя параметра: dataMember

1 Ответ

6 голосов
/ 28 января 2012

Как говорит msdn, вам нужно добавить TableName.ColumnName для того, чтобы связать элементы управления. Привязка форм Windows , вы можете попробовать вот так.

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