更新的时候执行两步:
先将输入cell的数据更新到数据库,然后再把刚输入的读出来,赋给另一个cell

解决方案 »

  1.   

    好像cell输入完成后没有事件可用!
      

  2.   

    这是我写一段代码不知道对你是否有用,在这里我有一个问题就是修改数量时,刚鼠标离开时响应失去焦点事件,但有键盘操作离开时,不知道什么原因,不响应这个事件
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace DataGrid
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.DataGrid dataGrid1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    private int price;
    private int num;
    private int currentRow;
    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
    this.SuspendLayout();
    // 
    // dataGrid1
    // 
    this.dataGrid1.AlternatingBackColor = System.Drawing.SystemColors.Window;
    this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.AppWorkspace;
    this.dataGrid1.CaptionFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
    this.dataGrid1.DataMember = "";
    this.dataGrid1.GridLineColor = System.Drawing.SystemColors.Control;
    this.dataGrid1.HeaderBackColor = System.Drawing.SystemColors.Control;
    this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dataGrid1.LinkColor = System.Drawing.SystemColors.HotTrack;
    this.dataGrid1.Location = new System.Drawing.Point(16, 16);
    this.dataGrid1.Name = "dataGrid1";
    this.dataGrid1.SelectionBackColor = System.Drawing.SystemColors.ActiveCaption;
    this.dataGrid1.SelectionForeColor = System.Drawing.SystemColors.ActiveCaptionText;
    this.dataGrid1.Size = new System.Drawing.Size(656, 360);
    this.dataGrid1.TabIndex = 0;
    this.dataGrid1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.dataGrid1_KeyPress);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(688, 389);
    this.Controls.Add(this.dataGrid1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    dt = ds.Tables.Add("myTable");
    DataColumn myColumn1 = new DataColumn();
    myColumn1.DataType = System.Type.GetType("System.Decimal");
    myColumn1.Caption = "单价";
    myColumn1.ColumnName = "单价";
    myColumn1.DefaultValue = "25";
    dt.Columns.Add(myColumn1);
    DataRow myRow1;
    for(int i = 0; i < 10; i++)

    myRow1 = dt.NewRow(); 
    myRow1["单价"] = i + 1; 
    dt.Rows.Add(myRow1); 
    } DataColumn myColumn2 = new DataColumn();
    myColumn2.DataType = System.Type.GetType("System.Decimal");
    myColumn2.Caption = "数量";
    myColumn2.ColumnName = "数量";
    myColumn2.DefaultValue = "2";
    dt.Columns.Add(myColumn2);
    DataRow myRow2;
    for(int i = 0; i < 10; i++)

    myRow2 = dt.NewRow(); 
    myRow2["数量"] =i+2;
    dt.Rows.Add(myRow2); 
    } DataColumn myColumn3 = new DataColumn();
    myColumn3.DataType = System.Type.GetType("System.Decimal");
    myColumn3.Caption = "总价";
    myColumn3.ColumnName = "总价";
    myColumn3.DefaultValue = 0;
    dt.Columns.Add(myColumn3);
    DataRow myRow3;
    for(int i = 0; i < 10; i++)

    myRow3 = dt.NewRow(); 
    myRow3["总价"] =0; 
    dt.Rows.Add(myRow3); 
    } DataGridTableStyle ts = new DataGridTableStyle();
    DataGridTextBoxColumn aColumnTextColumn = new DataGridTextBoxColumn();
    dataGrid1.DataSource = dt;
    ts.MappingName = dt.TableName; int numCols;
    numCols = dt.Columns.Count;
    dataGrid1.CaptionText = "test"; int j = 0;
    while(j<numCols)
    {
    aColumnTextColumn = new DataGridTextBoxColumn();
    aColumnTextColumn.HeaderText = dt.Columns[j].ColumnName;
    aColumnTextColumn.MappingName = dt.Columns[j].ColumnName;
    if (j==1)
    {
    aColumnTextColumn.TextBox.LostFocus +=new EventHandler(TextBox_LostFocus);
    aColumnTextColumn.TextBox.Enter +=new EventHandler(TextBox_Enter);
    aColumnTextColumn.TextBox.Leave +=new EventHandler(TextBox_Leave);
    }
    // if (j==1)
    // {
    ts.PreferredColumnWidth = 100;
    ts.PreferredRowHeight = 25;
    // }
    ts.AlternatingBackColor = Color.LightGray;
    ts.GridColumnStyles.Add(aColumnTextColumn);
    j = j+1;
    }// DataGridBoolColumn aColumnBoolColumn = new DataGridBoolColumn();
    // aColumnBoolColumn.HeaderText = dt.Columns[2].ColumnName;
    // aColumnBoolColumn.MappingName = dt.Columns[2].ColumnName;
    // ts.PreferredColumnWidth = 100;
    // ts.PreferredRowHeight = 25;
    // ts.AlternatingBackColor = Color.LightGray;
    // ts.GridColumnStyles.Add(aColumnBoolColumn);
    dataGrid1.TableStyles.Add(ts);
    } private void dataGrid1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    //MessageBox.Show("你输入的是"+e.KeyChar.ToString());
    }
    private void TextBox_Enter(object sender,System.EventArgs e)
    {
    dt.Rows[currentRow][2] = num * price;
    dt.Rows[currentRow][1] = num;
    }
    private void TextBox_Leave(object sender,System.EventArgs e)
    {
    dt.Rows[currentRow][2] = num * price;
    dt.Rows[currentRow][1] = num;
    }
    private void TextBox_LostFocus(object sender,System.EventArgs e)
    {
    num = Convert.ToInt32(((DataGridTextBoxColumn)this.dataGrid1.TableStyles[0].GridColumnStyles[1]).TextBox.Text);
    currentRow = dataGrid1.CurrentCell.RowNumber;
    price=Convert.ToInt32(dt.Rows[currentRow][0].ToString());
    //MessageBox.Show(price.ToString());
    //MessageBox.Show(num.ToString());

    //int currentRow = dataGrid1.CurrentCell.RowNumber;
    //dt.Rows[currentRow][0] = num * Convert.ToInt32(dt.Rows[currentRow][0]);
    //MessageBox.Show(price.ToString());
    //MessageBox.Show(num.ToString());
    // TextBox column = new TextBox();
    // DataGridTextBoxColumn TempColumn = new DataGridTextBoxColumn();
    // TempColumn = dataGrid1.TableStyles[0].GridColumnStyles[0];
    // column = TempColumn.TextBox;
    // MessageBox.Show(column.Text.ToString());
    } }
    }