小弟做了一個很簡單的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;
}
}
}
}
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;
}
}
}
}
-------------
da中应当有updatecommand的存在,它是靠那个来更新的.
{
MessageBox.Show("資料尚未修改");
return;
}
DataSet tempDataSet;
tempDataSet = ds.GetChanges(DataRowState.Modified);
if (tempDataSet.HasErrors)
{
MessageBox.Show("出現錯誤");
return;
} da.Update(tempDataSet);
來測試,發現彈出的信息是("資料尚未修改")
后面少代码SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(scd);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
我的解决办法是``每次datagridview进行更新或显示时``都进行datagridview.foucse()操作``
这些问题``微软自己是知道的``也确实是bug``msdn上微软给出的结论是``希望程序员自己通过其他操作间接解决这个问题``而且说他们也没有其他更好的解决方法``
如果是用toolStripButton的情况下``整个datagridview无法提交数据的话(也就是第一种问题)``我的解决办法就是在相应的toolStripButton的MouseEnter事件中把焦点切换到其他可以获取焦点的控件上``