为什么这个控件不能将结果保存回数据库?

时间:2012-07-16 17:02:44

标签: c# winforms visual-studio-2010 sql-server-ce

以下是表单:

enter image description here

以下是它背后的代码:

public partial class uxFormDatabase : Form 
{ 

BindingSource rawtableBindingSource = null; 

public uxFormDatabase(BindingSource myPassedSource)  
{ 
    InitializeComponent(); 
    rawtableBindingSource = myPassedSource; 

    uxDGVtable.AutoSize = true; 
    dataToDGV(); 
} 

public void dataToDGV() 
{ 
    uxrawdataBindingNavigator.BindingSource = this.rawtableBindingSource; 
    uxDGVtable.DataSource = this.rawtableBindingSource; 
} 

private void saveToolStripButton_Click(object sender, EventArgs e) 
{ 
    Validate(); 
    rawtableBindingSource.EndEdit(); 
} 

} 

我的印象是,当uxFormDatabase被激活时,方法public uxFormDatabase(BindingSource myPassedSource)会触发从父窗体传递BindingSource;

如果是这种情况,那么当我在BindingNavigator上按下保存按钮'saveToolStripButton_Click'时,为什么更改没有保存回数据库?这两行不会将更改保存回数据库Validate();rawtableBindingSource.EndEdit();吗?

更新

上述表单是从表单加载的:

namespace WindFormAppRevisionHelper
{

    public partial class uxRevisionHelperForm : Form
    {

        public SqlCeConnection conn = null;
        public SqlCeDataAdapter da = null;
        public DataSet ds = null;
        BindingSource definitionsBindingSource = new BindingSource();

    public uxRevisionHelperForm()
    {
        InitializeComponent();
        uxDescriptionTextBox.AutoSize = true;
        refreshBindingSource();
        assignControlsToSource();
    }

    public void refreshBindingSource()
    {            
        conn = new SqlCeConnection(ConfigurationManager.ConnectionStrings["WindFormAppRevisionHelper.Properties.Settings.DefinitionsDBConnectionString"].ConnectionString);
        da = new SqlCeDataAdapter(new SqlCeCommand("Select * From tb_RevisionDefinitions",conn));
        ds = new DataSet("Study Helper");
        ds.Tables.Add("DefinitionsTable");
        da.Fill(ds.Tables["DefinitionsTable"]);

        // Assign the BindingSource.
        definitionsBindingSource.DataSource = ds.Tables["DefinitionsTable"];
        uxBindingNavigator.BindingSource = this.definitionsBindingSource;

    }
    void assignControlsToSource() 
    {
        uxDescriptionTextBox.DataBindings.Add(new Binding("Text", definitionsBindingSource, "Description", true));
        uxWordPhraseTextBox.DataBindings.Add(new Binding("Text", definitionsBindingSource, "WordPhrase", true));
        uxReferenceTextBox.DataBindings.Add(new Binding("Text", definitionsBindingSource, "Reference", true));
    }

    private void uxUpdateDataButton_Click(object sender, EventArgs e)   
    {
        uxRevisionHelperGroupBox.Enabled = false;
        uxBindingNavigator.Hide();
        uxFormDatabase myNewDisplay = new uxFormDatabase(definitionsBindingSource);//<<<<this loads the _child_ form
        myNewDisplay.FormClosed += delegate { activateGroupBorder(); };
        myNewDisplay.Show();    
    }

    public void activateGroupBorder() 
    {
        uxRevisionHelperGroupBox.Enabled = true;
        uxBindingNavigator.Show();
    }
}

1 个答案:

答案 0 :(得分:1)

如果查看this example,您可以看到,用于准备绑定源的SqlDataAdapter对象负责数据库更新。

我认为您可以传递给uxFormDatabase的构造函数SqlCeDataAdapter da,将其保存在uxFormDatabase内的全局实例var中,然后在Validate之后和EndEdit,致电da.Update((DataTable)rawtableBindingSource.DataSource);

使用的代码:

    private void saveToolStripButton_Click(object sender, EventArgs e)
    {
        Validate();
        rawtableBindingSource.EndEdit();
        SqlCeCommandBuilder cmdBuilder = new SqlCeCommandBuilder(rawtableDA);
        rawtableDA.Update((DataTable)rawtableBindingSource.DataSource);
        cmdBuilder = null;
    }