小弟做了一個很簡單的DataGridView,然后在BindingNavigator做了一個保存按鈕,但是去老是保存不了DataGridView更改過的東西,還望高人賜教!小弟拜謝!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace pjb
{
    public partial class pjbUnit : Form
    {
        private DataSet ds = new DataSet();
        private DataTable dt = new DataTable();
        private BindingSource bs = new BindingSource();
        private SqlDataAdapter da = new SqlDataAdapter();
        private LinkDataBase link = new LinkDataBase();
       
        
        public pjbUnit()
        {
            InitializeComponent();
        }        private void pjbUnit_Load(object sender, EventArgs e)
        {
           
           string s1 = "SELECT * from pjb_product_unit";
            string s2 = "pjb_product_unit";
             DataTable dt = link.SelectDataBase(s1);
              DataSet ds = link.SelectDataBase(s1,s2);
              SqlConnection myConnection = new SqlConnection("workstation id=localhost;Integrated Security=SSPI;database=pjb");
              SqlCommand scd = new SqlCommand("select * from pjb_product_unit",myConnection);
               da.SelectCommand = scd;
               da.Fill(dt);
               unitDataGridView.DataSource = dt;
               bs.DataSource = ds;
               bs.DataMember = "pjb_product_unit";
               unitBindingNavigator.BindingSource = bs;
            
           
 
    
        }        private void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {                da.Update(ds.pjb_product_unit);
                
                MessageBox.Show("更新成功!");            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
                    }       
    }
}

解决方案 »

  1.   

     da.Update(ds.pjb_product_unit); 
    -------------
    da中应当有updatecommand的存在,它是靠那个来更新的.
      

  2.   

    應該不是這個問題啊,我用if (!ds.HasChanges())
                       {
                           MessageBox.Show("資料尚未修改");
                           return;
                       }
                       DataSet tempDataSet;
                       tempDataSet = ds.GetChanges(DataRowState.Modified);
                       if (tempDataSet.HasErrors)
                       {
                           MessageBox.Show("出現錯誤");
                           return;
                       }                   da.Update(tempDataSet);
    來測試,發現彈出的信息是("資料尚未修改")
      

  3.   

    SqlCommand scd = new SqlCommand("select * from pjb_product_unit",myConnection); 
    后面少代码SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(scd);
    SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
      

  4.   

    試過了,沒有用哦,怎么辦?已經試過了網上能查到的所有方法,有人說DataGridView是不能改變dataset的內容的,所以我就用dataAdapter.update(dataTable)的方法,可還是沒有用,大家幫小弟想想辦法啊。
      

  5.   

    謝謝大家啦!問題已經解決!其實問題的關鍵是因为toolStripButton是无法获取焦点的``所以每次点击它``datagridview的数据并没有被正常提交到DataTable   (注:datagridview在切换焦点后才能正常提交``普通button在点击的时候焦点自动切换了)``所以无法更新到数据库``   我的解决办法是在toolStripButton的mouseenter事件中进行焦点切换``   后来又遇到过类似问题`跟上面问题不太一样``但同样是焦点的问题``问题是这样的:datagridview的第一行无法提交到数据库``其他行却很正常``有了上面的经验``就去找焦点的原因``后来终于找到原因``在进行其他操作去更新或者显示datagridview时``datagridview会自动获取选中状态``这个是默认选中是在第一行的``但它并不是焦点``因为它没有提交数据的动作`` 
    我的解决办法是``每次datagridview进行更新或显示时``都进行datagridview.foucse()操作``   
    这些问题``微软自己是知道的``也确实是bug``msdn上微软给出的结论是``希望程序员自己通过其他操作间接解决这个问题``而且说他们也没有其他更好的解决方法`` 
    如果是用toolStripButton的情况下``整个datagridview无法提交数据的话(也就是第一种问题)``我的解决办法就是在相应的toolStripButton的MouseEnter事件中把焦点切换到其他可以获取焦点的控件上``