Note that there are some explanatory texts on larger screens.

plurals
  1. POTwo-way data binding with DataSet and ADO.NET
    primarykey
    data
    text
    <p>I'm just beginning to explore WPF and its possibilities regarding data binding.</p> <p>I've got a sample Access database file and put its content into a <code>DataSet</code>. Then I bind the <code>DataSet</code> to a Grid (two way binding). What I want to achieve is to update the underlying database entry of the changed <code>DataSet</code>-Item. This is my code:</p> <pre><code>public partial class MainWindow : Window { DataSet sampleDataSet; OleDbDataAdapter adapter; public MainWindow() { InitializeComponent(); InitializeDB(); } private void InitializeDB() { string mdbFile = "./test.mdb"; string connString = string.Format( "Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}", mdbFile); OleDbConnection conn = new OleDbConnection(connString); adapter = new OleDbDataAdapter("SELECT * FROM Player;", conn); // ---------------------- // **EDIT** inserted update command OleDbCommand cmd = new OleDbCommand("UPDATE Player SET Name = @Name " + "WHERE ID = @ID", conn); cmd.Parameters.Add("@Name", OleDbType.VarChar, 40, "Name"); cmd.Parameters.Add("@ID", OleDbType.Char, 5, "ID"); // set the update command adapter.UpdateCommand = cmd; // **End of EDIT** // ---------------------- sampleDataSet = new DataSet("Player Table"); adapter.Fill(sampleDataSet, "Player"); data1.DataContext = sampleDataSet; } private void data1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { if (e.EditAction == DataGridEditAction.Commit) { adapter.Update(sampleDataSet, "Player"); } } // Edit Comment April, 14th - Begin private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { //sampleDataSet.AcceptChanges(); adapter.Update(sampleDataSet, "Player"); // Calling update again fixes it, but why? } // Edit Comment April, 14th - End } </code></pre> <p>My XAML looks like this:</p> <pre><code>&lt;Window x:Class="AdoTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;DataGrid Name="data1" AutoGenerateColumns="True" ItemsSource="{Binding Mode=TwoWay, Path=Player}" CellEditEnding="data1_CellEditEnding" /&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>What is the approach to actually persist the changed values in the grid to the database file?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload