If the grid has been sorted, you can no longer get at the current row in the table through the grid's CurrentRowIndex. But for both unsorted and sorted grids, you can get at the current row through the BindingContext and the Current property of the BindingManagerBaseBindingManagerBase bm = this.dataGrid1.BindingContextr[this.dataGrid1.DataSource, this.dataGrid1.DataMember]; 
DataRow dr = ((DataRowView)bm.Current).Row; 
 

解决方案 »

  1.   

    Control 的 BindingContext 对象用于为 Control 包含的所有数据绑定控件返回单个 BindingManagerBase 对象。BindingManagerBase 对象使绑定到同一数据源的所有控件保持同步。例如,设置 BindingManagerBase 的 Position 属性将指定所有数据绑定控件所指向的基础列表中的项。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 a navigation path in the form: 
          "TableName.ColumnName". */
       text1.DataBindings.Add(new Binding
       ("Text", ds, "customers.custName"));
       text2.DataBindings.Add(new Binding
       ("Text", ds, "customers.custID"));
       
       /* Bind the DateTimePicker control by adding a new Binding. 
          The data member of the DateTimePicker is a navigation path:
          TableName.RelationName.ColumnName string. */
       DateTimePicker1.DataBindings.Add(new 
       Binding("Value", ds, "customers.CustToOrders.OrderDate"));   /* Add event delegates for the Parse and Format events to a 
          new 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);
       text3.DataBindings.Add(b);   // Get the BindingManagerBase for the Customers table. 
       bmCustomers = this.BindingContext [ds, "Customers"];   /* Get the BindingManagerBase for the Orders table using the 
          RelationName. */ 
       bmOrders = this.BindingContext[ds, "customers.CustToOrders"];   /* Bind the fourth TextBox control's Text property to the
       third control's Text property. */
       text4.DataBindings.Add("Text", text3, "Text");
    }
    (帮助中的代码)