更新动态链接到DataGrid的数据库

时间:2016-11-10 15:20:03

标签: c# wpf database datagrid

我想在我的WPF表单上放置几个按钮并使用来自不同数据库的数据为每个按钮动态更新我的DataGrid,比方说,我有3个数据库和3个按钮,当你按下一个按钮就可以得到数据来自1个不同的数据库。

但我希望DataGrid上的数据在网格上进行更改时更新回数据库,因此我不必编写所有" save"东西。

现在,我的代码是:

try
        {
            OleDbConnection conn = new OleDbConnection(
                "Provider=Microsoft.Jet.OLEDB.4.0; " +
                "Data Source=MaterialDB.mdb");
            OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM myTable", conn);

            myDataSet = new DataSet();
            adapter.Fill(myDataSet, "myTable");

            var be = CatalogsGrid.GetBindingExpression(DataGrid.ItemsSourceProperty);

            CatalogsGrid.DataContext = myDataSet;
            CatalogsGrid.ItemsSource = myDataSet.Tables[0].DefaultView;
            Binding nbe = new Binding();
            nbe.Source = myDataSet;
            nbe.Mode = BindingMode.TwoWay;
            nbe.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            nbe.Path = new PropertyPath("myTable");
            nbe.BindsDirectlyToSource = true;
            CatalogsGrid.SetBinding(DataGrid.ItemsSourceProperty, nbe);


            CatalogsGrid.Items.Refresh();

        }

此代码将数据从数据库加载到网格中,但我编辑数据并且它不会更新回MS Access表。

哦,它仍然没有编码,但是" myTable"应该是一个字符串,我在其中放置我想要显示的不同表的名称并从网格中更新。

我错过了什么?

3 个答案:

答案 0 :(得分:1)

DataGrid不会自动保存更改,由您(您的应用程序)决定,但它会将更改写回基础DataTable。

据我了解,您希望避免使用保存按钮,并在更改后立即将更改保存回数据库。您现在需要知道什么时候发生了变化,您的DataSet可以帮助您:

myDataSet = new DataSet();
myDataTable = myDataSet.Tables["myTable"];
adapter.Fill(myDataTable);

// Update the DB whenever a row changes
myDataTable.OnRowChanged += (s, e) =>
{
    adapter.Update(myDataTable);
}

答案 1 :(得分:0)

创建一个集中式方法,用于更新用于绑定数据的数据。创建一个保存按钮并调用以下功能。它将节省网络流量,以便在每次更改时保存数据。

public void UpdateDb ( OleDbDataAdapter adap, DataSet ds)
{
    var build= new OleDblCommandBuilder (adap);
    adap.Update (ds);
}

只需要传递参数,它就会更新对数据集所做的更改。

答案 2 :(得分:0)

以@Bernhard的答案为出发点,不得不添加更新命令和一些调整,它现在正在运行。

这是最终代码:

try
        {
            OleDbConnection conn = new OleDbConnection(
                "Provider=Microsoft.Jet.OLEDB.4.0; " +
                "Data Source=MaterialDB.mdb");
            //conn.Open();
            OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM myTable", conn);

            myDataSet = new DataSet();
            adapter.Fill(myDataSet, "myTable");
            DataTable myDataTable = myDataSet.Tables["myTable"];

            // Create the UpdateCommand.
            OleDbCommand command = new OleDbCommand(
                "UPDATE myTable SET Type = @Type, Description = @Description " +
                "WHERE ID = @ID", conn);

            // Add the parameters for the UpdateCommand.
            command.Parameters.Add("@Type", OleDbType.Char, 255, "Type");
            command.Parameters.Add("@Description", OleDbType.Char, 255, "Description");
            command.Parameters.Add("@ID", OleDbType.Integer, 5, "ID");
            adapter.UpdateCommand = command;

            // Update the DB whenever a row changes
            myDataTable.RowChanged += (s, f) =>
            {
                adapter.Update(myDataTable);
            };
            var be = CatalogsGrid.GetBindingExpression(DataGrid.ItemsSourceProperty);

            CatalogsGrid.DataContext = myDataSet;
            CatalogsGrid.ItemsSource = myDataSet.Tables[0].DefaultView;
            Binding nbe = new Binding();
            nbe.Source = myDataSet;
            nbe.Mode = BindingMode.TwoWay;
            nbe.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            nbe.Path = new PropertyPath("myTable");
            nbe.BindsDirectlyToSource = true;
            CatalogsGrid.SetBinding(DataGrid.ItemsSourceProperty, nbe);


            CatalogsGrid.Items.Refresh();

        }

感谢您的帮助。

相关问题