1

这是窗体:

在此处输入图像描述

这是它背后的代码:

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();
    }
}
4

1 回答 1

1

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

我认为你可以传递给你的构造函数uxFormDatabaseSqlCeDataAdapter da将它保存在你的全局实例变量中uxFormDatabase,然后在Validateand之后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;
    }
于 2012-07-16T19:32:02.043 回答