DataGridView的一行包括许多单元格
其中里面可以编辑数据
怎样让焦点一离开这一行的时候,自动更新数据到数据库?
最好能提供代码!!!

解决方案 »

  1.   

    private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
            {
                string connstr = "user id=manager;password=foton;data source=ecudb";
                using (System.Data.OracleClient.OracleConnection conn = new System.Data.OracleClient.OracleConnection(connstr))
                {
                    DataSet ds = new DataSet();
                    string selectSql = "update table1 set t_orderinfo.goods='商品名称' where ...条件(根据你gridview上边某个唯一的条件判断)";
                    System.Data.OracleClient.OracleCommand cmd = new System.Data.OracleClient.OracleCommand(selectSql, conn);
                    cmd.ExecuteNonQuery();
                }
            }
      

  2.   

    离开这一行有点麻烦。如果只是编辑完了就要更新数据的话,那可以用CellEndEdit事件,在其中连接数据库操作。
      

  3.   

    这样就可以了:
     private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
            {
                string connstr = "user id=manager;password=foton;data source=ecudb";
                using (System.Data.OracleClient.OracleConnection conn = new System.Data.OracleClient.OracleConnection(connstr))
                {
                    DataSet ds = new DataSet();
                    string selectSql = "update table1 set t_orderinfo.goods='商品名称' where ...条件(根据你gridview上边某个唯一的条件判断)";
                    System.Data.OracleClient.OracleCommand cmd = new System.Data.OracleClient.OracleCommand(selectSql, conn);
                    cmd.ExecuteNonQuery();
                }
            }
      

  4.   

    注册RowLeave事件。
    dataGridView1.RowLeave += new DataGridViewCellEventHandler(dataGridView1_RowLeave);
    void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
            {
                
                if (this.dataGridView1.IsCurrentRowDirty)
                {
                    this.dataGridView1.EndEdit();
                    Save(e.RowIndex);
                }        }
    private void Save(int rowIndex)
            {......}
      

  5.   

    搂主需要给每行加个标志位,如果这一行被修改了,那么设置该行的标志位某一数值比如True
    然后,当用户选择其它行时,判断这个标志位,如果为True那么更新到数据库,这样是比较常用的做法标志位搂主可以用DataGridViewRow类的Tag属性这样做的好处是,用户修改完该行的所有单元格时才会提交到数据库;
    否则如果每修改一个单元格就提交的话,有可能数据不完整,数据访问也太频繁
      

  6.   

    就是下面代码的意思,搂主参考一下:private DataGridViewRow m_PrevSelectedRow = null;//SelectionChanged事件是行之间切换选中的时候触发的
    private void dataGridView1_SelectionChanged(object sender, EventArgs e)
    {
         if (this.m_PrevSelectedRow != null && this.m_PrevSelectedRow.Tag != null)
         {
             if (this.m_PrevSelectedRow.Tag.Equals(true))
             {
                  //在这里,开始更新你的数据表          }
         }
         this.m_PrevSelectedRow = this.dataGridView1.CurrentRow;
    }
      

  7.   

    哦,还少一个事件代码,就是在单元格改变的时候,修改标志位:
    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        DataGridViewRow currRow = this.dataGridView1.Rows[e.RowIndex];
        DataGridViewCell currCell = currRow.Cells[e.ColumnIndex];
        if (currRow.Tag == null || !currRow.Tag.Equals(true))
            currRow.Tag = true;
    }
      

  8.   

    哦,还少一个事件代码,就是在单元格改变的时候,修改标志位:
    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        DataGridViewRow currRow = this.dataGridView1.Rows[e.RowIndex];
        DataGridViewCell currCell = currRow.Cells[e.ColumnIndex];
        if (currRow.Tag == null || !currRow.Tag.Equals(true))
            currRow.Tag = true;
    }
      

  9.   

            if (this.m_PrevSelectedRow.Tag.Equals(true)) 
            { 
                  //在这里,开始更新你的数据表 注意:这里是把行:this.m_PrevSelectedRow里面的各单元格的数据提取出来,然后提交到数据库
    提取就不用多说了:
    单元格的值=this.m_PrevSelectedRow.Cells[index].Value;