现在DataGridView与DataSet是用BindingSource关联在一起的。在DataGridView新行中的某Cells里添入值后,DataSet的Tables中并没有些值(也没有此行)。
离开此Cells,但不离开此Rows时,DataSet仍没有此值。
离开此Rows后,DataSet中出现此值。
我想在不离开此Rows的前提下,DataSet中出现此值,应该怎么办?
用了“dataGridView0.EndEdit();”和“bindingSource0.EndEdit();”没有什么反应。
其实换一个问法就是:如何用代码的方式,将DataGridView中“还没提交给DataSet的数据”强制提交给DataSet。

解决方案 »

  1.   

    http://topic.csdn.net/u/20080602/13/810b39fc-84a7-4038-ba02-aa4814b43745.html
      

  2.   

    单元格内的文本有变动的时候,立即对Dataset进行刷新!
      

  3.   

    我知道DataGridView与DataSet用BindingSource设定绑定后,对DataGridView的更改会自动改变DataSet。
    但有时会有一个延迟。
    比如:在新行中添加了数据后,如果没有离开此行,DataSet就不会添加这一行。
    再比如:在textBox4_TextChanged方法里,去找textBox4所绑定的DataSet数据,会发现在这一瞬间DataSet中数据并没有变化。
    我知道从“数据库”刷新到Dataset的方法,但从DataGridView刷新到Dataset的方法应该怎么做呢?
      

  4.   

    dataGridView1.CommitEdit(System.Windows.Forms.DataGridViewDataErrorContexts.Commit);
      

  5.   

    http://topic.csdn.net/u/20080602/13/810b39fc-84a7-4038-ba02-aa4814b43745.html
      

  6.   

    这个链接的标题与我的一样,但回答的内容是DataSet向数据库的更新
      

  7.   

    我用实际应用的方式重新提一下这个问题。
    dataGridView1与ds0.Tables[1]之前已绑定,绑定的代码大致如下:
    ……
    bindingSource0.DataSource = ds0;
    bindingSource0.DataMember = "TableParent";
    bindingNavigator1.BindingSource = bindingSource0;
    dataGridView0.DataSource = bindingSource0;
    dataGridView1.DataSource = bindingSource0;
    dataGridView1.DataMember = "drn1Name";
    ……现在有以下代码:protected override void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        MessageBox.Show(ds0.Tables[1].Rows.Count.ToString());
    }假设目前ds0.Tables[1].Rows.Count = 5
    情况1:在dataGridView1现有Row的任何Cell里写入数据,弹出MessageBox都会显示“5”。
    情况2:在dataGridView1新增Row的Cells[0]里写入数据,弹出MessageBox会显示“5”。
           不离开当前行,直接在Cells[1]里写入数据,弹出MessageBox还会显示“5”。
    情况3:在dataGridView1新增Row的Cells[0]里写入数据,弹出MessageBox会显示“5”。
           离开一下当前行,再在Cells[1]里写入数据,弹出MessageBox会显示“6”。
          (离开一下当前行可以是用光标点一下上一行的内容等,以使当前行退出编辑)我现在想让“情况2”在不离开当前行的前提下,弹出MessageBox显示“6”。
    应该在如何操作?
      

  8.   

    如果没有将焦点离开过编辑行,编辑的内容不会被存入DataSet或者DataTable,可以尝试一下焦点的切换,比如在保存的时候把输入焦点从DataGridView移动到别的控件上