本人首次使用C#做设计,许多问题觉得难以解决,本次提问是关于DataGrid的数据邦定问题,虽然知道在论坛的旧帖中有许多类似的问题,但是时间比较紧没时间去找,想直接能有可参考的代码,问题描述如下:
    在一个winForm中有一个DataGrid,其数据是在程序运行时逐条添加的(可以是用户在其他textBox中输入的),在该窗体中还有一些(比如说两个textBox),这时我的鼠标点击DataGrid的某一行,使得textBox中的值相应的改变,然后我再textBox中修改该值最好能体现到DataGrid中,以及保存到数据库中。   问题描述完毕,先抱歉没能充分利用论坛的旧帖资源,谢谢大家的。
时间比较紧,希望大家能尽快给可运行的代码,有附带数据库演示的请附带上数据库(SQL和Access都行)

解决方案 »

  1.   

    textBox.DataBindings.Add("Text",数据源,"字段名");
      

  2.   

    这个使用DataBindings
    论坛里这类问题实在是多阿
    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"];
    }bmOrders.Position属性控制游标
    private void GetBindingManagerBase()
    {
       /* CustomersToOrders is the RelationName of a DataRelation. 
       Therefore, the list maintained by the BindingManagerBase is the
       list of orders that belong to a specific customer in the 
       DataTable named Customers, found in DataSet1. */
       myBindingManagerBase = 
       this.BindingContext[DataSet1, "Customers.CustomersToOrders"];   // Adds delegates to the CurrentChanged and PositionChanged events.
       myBindingManagerBase.PositionChanged += 
       new EventHandler(BindingManagerBase_PositionChanged);
       myBindingManagerBase.CurrentChanged +=
       new EventHandler(BindingManagerBase_CurrentChanged);
    }private void BindingManagerBase_PositionChanged
    (object sender, EventArgs e)
    {
       // Prints the new Position of the BindingManagerBase.
       Console.Write("Position Changed: ");
       Console.WriteLine(((BindingManagerBase)sender).Position);
    }private void BindingManagerBase_CurrentChanged
    (object sender, EventArgs e)
    {
       // Prints the new value of the current object.
       Console.Write("Current Changed: ");
       Console.WriteLine(((BindingManagerBase)sender).Current);
    }private void MoveNext()
    {
       // Increments the Position property value by one.
       myBindingManagerBase.Position += 1;
    }private void MovePrevious()
    {
       // Decrements the Position property value by one.
       myBindingManagerBase.Position -= 1;
    }private void MoveFirst()
    {
       // Goes to the first row in the list.
       myBindingManagerBase.Position = 0;
    }private void MoveLast()
    {
       // Goes to the last row in the list.
       myBindingManagerBase.Position = 
       myBindingManagerBase.Count - 1;
    }
    给你贴的是MSDN里面的例子,人总是要靠自己的
      

  3.   

    假如有一个dataSet1,其中有一个table1.在系统中你如果把表格控件和文本控件都与同一个表作数据绑定就能达到你的目的.数据表格的绑定是
    dataGrid1.SetDataBinding(dataSet1,"table1");如果有一textBox控件textIP,它要与table1的ip字段绑定则代码如下
    textIP.DataBindings.Add("Text",dataSet1,"table1.ip");1)这时我的鼠标点击DataGrid的某一行,使得textBox中的值相应的改变,
    只要改变dataGrid1.Select属性文本框中的内容就会随之改变,难点是dataGrid1要响应鼠标单击事件你必需编写代码.
    private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
       if (e.Button==MouseButtons.Right) return;
       Point pt = new Point(e.X, e.Y);
       DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);
       if(hti.Type == DataGrid.HitTestType.Cell)
       {
          dataGrid1.Select(hti.Row);
          dataGrid1.CurrentRowIndex=hti.Row;
       }   
    }2)我在textBox中修改该值最好能体现到DataGrid中
    这时你对textIP的修改就是对dataGrid1的修改,因为这时你修改textIP的值实际就是修改table1的记录,同时因为dataSet1已与table1绑定,则dataSet1中的值会随table1的变化相应的改变.3)以及保存到数据库中。
    请参阅ADO.net,
      

  4.   

    1、绑定,需要用同一个数据源,例如:
    DataView dv = yourDataTable.DefaultView;
    yourDataGrid.DataSource = dv;
    yourTextBox.DataBindings.Add( "Text", dv, yourFieldName);2、参看
    http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c43c.aspx#q1017q
      

  5.   

    wxy0401(abc) 
     
    谢谢你的回答,我按你说的方法可以达到数据绑定,数据库的链接我是过sqlDataAdaper的向导来实现的,在这过程中添加了一个表m,然后根据生成的 sqlDataAdaper 以及 sqlConnection1,然后我在sqlDataAdaper1(设计面板中的)右键   选项中有一个是 生成数据集 该数据集新建了个DataSet1我的页面有两个 textBox对象现在我再form_load中写这样的代码:dataGrid1.SetDataBinding(dataSet11,"m");
    this.sqlDataAdapter1.Fill(this.dataSet11,"m");
    textBox1.DataBindings.Add(new Binding
    ("Text", dataSet11, "m.Expr1")
    );
    textBox2.DataBindings.Add(new Binding
    ("Text", dataSet11, "m.Expr2")
    );------------
    这样我在运行时   可以  将数据绑定,然后我按你说的 给 dataGrid 加了鼠标事件,然后就是我点dataGrid的某一行,该行就以标亮的形式显示,也就是选中了一整行,但是这时假如我点到了空行,就是在dataGrid中最下面的那一行没有数据的那个,这是就出现错误了,我想应该是该行还不存在,所以无法与其它控件进行绑定,
    我想能不能点到那行空行时,给改dataGrid新加一行,textBox 中的数据就显示空白啊?
      

  6.   

    续上(自己的回复)请问这时我假如想要将数据保存到数据库中,可以同dataAdaper 的Update方法吗?
      

  7.   

    http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c43c.aspx#q1017q
      

  8.   

    文本框使用单值绑定,
    http://blog.csdn.net/zhzuo/archive/2005/01/03/238273.aspx