【问题标题】:How to refresh the bindingnavigator binding source in c#?如何在c#中刷新bindingnavigator绑定源?
【发布时间】:2015-07-12 05:53:57
【问题描述】:

从其他表单更改数据库中的数据后,我在刷新bindingsource 时遇到问题。现在,当我第一次运行我的程序时,所有数据都显示在textboxes 中,bindingnavigator 与数据库具有相同的记录。话虽如此,我正在尝试以不同于包含bindingnavigator 的形式从数据库中添加或删除数据。当我关闭其他表单并返回bindingnavigator 表单时,dataset 没有更新,它只显示应用程序上一次运行的数据...

this.tblEmployeeTableAdapter.Fill(this.employeePayDatabaseDataSet.tblEmployee);

TableAdapterFill() 方法仅在我运行程序时有效,我尝试在其他方法中实现它但它不会刷新我的dataset。即使我关闭表单并重新打开它,知道 dataset 会在 Form_Load() 方法上加载。

我试图在按钮上创建一个重新加载方法,它以某种方式设置 bindingnavigator binding sourcenull 但没有数据显示!!!

private void bindingNavigatorReload_Click(object sender, EventArgs e)
        {
            EmployeePayDatabaseDataSetTableAdapters.tblEmployeeTableAdapter NewtblAdapter = new EmployeePayDatabaseDataSetTableAdapters.tblEmployeeTableAdapter();
            EmployeePayDatabaseDataSet NewDataSet = new EmployeePayDatabaseDataSet();
            NewtblAdapter.Fill(NewDataSet.tblEmployee);

        }

提示:

databaseCopy to output Directory 属性设置为 Copy Always

datasetCopy to output Directory 属性设置为Do Not Copy

我将SqlServer 2008 用于数据库,visual studio 2010 用于项目。数据库是service-based 数据库,用于数据库的模型是Entity Model

【问题讨论】:

    标签: c# dataset bindingsource tableadapter bindingnavigator


    【解决方案1】:

    好吧,既然没人能找到这个问题的解决方案,我决定自己做......

    首先,我必须从window properties 中的所有controls 中删除data bindings,这样我才能以编程方式创建它们。然后我必须实现一个从我的textboxes 中清除所有data bindings 的方法,最后执行UpdateBindingNavigator() 方法...

    在开始之前,只需在命名空间中定义这两个变量;

    SqlDataAdapter datapter;
    DataSet dset
    

    string connection = "your_connection_string";

    private void ClearBeforeFill()
    {
    txtbox1.DataBindings.Clear();
    txtbox2.DataBindings.Clear();
    txtbox3.DataBindings.Clear();
    txtbox4.DataBindings.Clear();
    }
    
    private void UpdateBindingNavigator()
    {
    ClearBeforeFill();
    datapter = new SqlDataAdapter("SELECT * FROM tblEmployee", connection);
    dset = new DataSet();
    datapter.Fill(dset);
    
    BindingSource1.DataSource = dset.Tables[0];
    
    txtbox1.DataBindings.Add(new Binding("Text", BindingSource1, "Emp_ID", true));
    txtbox2.DataBindings.Add(new Binding("Text", BindingSource1, "Emp_Name", true));
    txtbox3.DataBindings.Add(new Binding("Text", BindingSource1, "Emp_Age", true));
    txtbox4.DataBindings.Add(new Binding("Text", BindingSource1, "Emp_Salary", true));
    
    }
    

    最后你可以从任何你想调用的方法UpdateBindingNavigator() 它会用新的数据刷新你的数据!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-28
      • 2011-09-16
      • 2012-02-19
      • 2013-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多