我似乎无法绑定到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
答案 0 :(得分:6)
正如msdn所说,你需要添加TableName.ColumnName才能绑定控件。 Windows Form Binding ,您可以尝试这样做。
textBoxShipperName.DataBindings.Add(new Binding("Text", bindingSourceMain, "Declarations.ShipperName", true))