using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
namespace SimpleBinding
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string connstring = @"
                server=.\sqlexpress;
                integrated security=true;
                database=northwind";
            string sql = @"
                select
                *
                from
                employees";
            SqlConnection conn = new SqlConnection(connstring);
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "employee");            textBox1.DataBindings.Add("Text", ds, "employee.firstname");
            textBox2.DataBindings.Add("text", ds, "employee.lastname");            
        }
    }
}
就最后两行DataBingings下面有两个红色下划线,然后说错误 1 “System.Windows.Controls.TextBox”不包含“DataBindings”的定义,并且找不到可接受类型为“System.Windows.Controls.TextBox”的第一个参数的扩展方法“DataBindings”(是否缺少 using 指令或程序集引用?)
但是我加了using System.Windows.Forms和dll引用的。不知道怎么回事。

解决方案 »

  1.   

    你这是要干什么?
    要给Textbox赋值直接用textbox1.text=ds.Table[0][firstname].tostring();
      

  2.   

    呼叫楼上,请教个问题呀你好,刚看到您一个回复,dev控件LookUpEdit怎么增加一列复选框。但我不知道具体怎么你能告诉我吗?谢谢。qq:20079257
      

  3.   

    textBox1.DataBindings ,没有DataBindings,直接赋值即可。
      

  4.   

    DataBindings属性在.net 1.0中就已经有了,但对于Client Profile,只在3.5 SP1以后提供,所以我想你应该是选错了.net框架的版本。
      

  5.   

    谢谢两位,我这是照书上的,但是MSDN上有这个http://msdn.microsoft.com/zh-cn/library/system.windows.forms.control.databindings.aspx
    protected void BindControls()
    {
       /* Create two Binding objects for the first two TextBox 
       controls. The data-bound property for both controls 
       is the Text property. The data source is a DataSet 
       (ds). The data member is specified by a navigation 
       path in the form : TableName.ColumnName. */
       textBox1.DataBindings.Add(new Binding
       ("Text", ds, "customers.custName"));
       textBox2.DataBindings.Add(new Binding
       ("Text", ds, "customers.custID"));   /* Bind the DateTimePicker control by adding a new Binding. 
       The data member of the DateTimePicker is specified by a 
       navigation path in the form: TableName.RelationName.ColumnName. */
       DateTimePicker1.DataBindings.Add(new 
       Binding("Value", ds, "customers.CustToOrders.OrderDate"));   /* Create a new Binding using the DataSet and a 
       navigation path(TableName.RelationName.ColumnName).
       Add event delegates for the Parse and Format events to 
       the Binding object, and add the object to the third 
       TextBox control's BindingsCollection. The delegates 
       must be added before adding the Binding to the 
       collection; otherwise, no formatting occurs until 
       the Current object of the BindingManagerBase for 
       the data source changes. */
       Binding b = new Binding
       ("Text", ds, "customers.custToOrders.OrderAmount");
       b.Parse += new ConvertEventHandler(CurrencyStringToDecimal);
       b.Format += new ConvertEventHandler(DecimalToCurrencyString);
       textBox3.DataBindings.Add(b);   /*Bind the fourth TextBox to the Value of the 
       DateTimePicker control. This demonstrates how one control
       can be bound to another.*/
       textBox4.DataBindings.Add("Text", DateTimePicker1,"Value");
       BindingManagerBase bmText = this.BindingContext[
       DateTimePicker1];   /* Print the Type of the BindingManagerBase, which is 
       a PropertyManager because the data source
       returns only a single property value. */
       Console.WriteLine(bmText.GetType().ToString());
       // Print the count of managed objects, which is 1.
       Console.WriteLine(bmText.Count);   // Get the BindingManagerBase for the Customers table. 
       bmCustomers = this.BindingContext [ds, "Customers"];
       /* Print the Type and count of the BindingManagerBase.
       Because the data source inherits from IBindingList,
       it is a RelatedCurrencyManager (derived from CurrencyManager). */
       Console.WriteLine(bmCustomers.GetType().ToString());
       Console.WriteLine(bmCustomers.Count);   /* Get the BindingManagerBase for the Orders of the current
       customer using a navigation path: TableName.RelationName. */ 
       bmOrders = this.BindingContext[ds, "customers.CustToOrders"];
    }
      

  6.   

    顶1L回答,textbox的赋值只要把对应数据赋给text属性就可以了
      

  7.   

    但我属性里面看目标框架是.Net Famework 4 Client Profile
      

  8.   

    但MSDN上 有这个啊:  textBox1.DataBindings.Add(new Binding
       ("Text", ds, "customers.custName"));
      

  9.   

    改用.Net Framework 4试试
      

  10.   

    请教effun,怎么为控件制定绑定?
      

  11.   

    你的TextBox应该是WPF中的TextBox吧            //WPF中的TextBox
                System.Windows.Controls.TextBox tbWpf = new System.Windows.Controls.TextBox();
                tbWpf.SetBinding(TextBox.TextProperty, "yourBind");
                
                //WinForm程序中的TextBox
                System.Windows.Forms.TextBox tbForm = new System.Windows.Forms.TextBox();
                tbForm.DataBindings.Add(new System.Windows.Forms.Binding());