I have a editable XamDataGrid bound to a Dataset as shown:
<Custom:XamDataGrid x:Name="usersXamDataGrid" DataSource="{Binding}" Margin="150,400,150,-0.2" />
It is being filled using SqlDataAdapter:
using (SqlConnection conn = new SqlConnection(connectionString)) { SqlCommand usersSelectCommand = new SqlCommand("SELECT name, age FROM Users", conn); SqlDataAdapter usersAdapter = new SqlDataAdapter(usersSelectCommand); usersAdapter.Fill(usersDataSet, "Users"); }
Then binding the dataset to the grid's datasource:
this.usersXamDataGrid.DataSource = usersDataSet.Tables["Users"].DefaultView;
What's a good way to save changes to edited cells in the XamDataGrid?
I got it updating the database, thanks!
Hello Michael,
You might want to try calling Open on the connection string and then Close between updates. Please refer to this help article that discusses this more in detail.
Okay do you see anything wrong with doing this? It's not updating the database but I feel like I'm close.
string connectionString = DatabaseConnectionString; DataSet usersDataSet = new DataSet("usersDataSet"); SqlDataAdapter daSelectUsers; SqlCommand daUpdateUsers; using (SqlConnection conn = new SqlConnection(connectionString)) { daSelectUsers = new SqlDataAdapter("SELECT Name, Age FROM Users, conn); foreach (DataRecord record in usersXamDataGrid.Records) { daUpdateUsers = new SqlCommand("UPDATE Users SET Name = '" + record.Cells["Name"].Value + "'," + "Age = '" + record.Cells["Age"].Value + "'", conn); daSelectUsers.UpdateCommand = daUpdateUsers; daSelectUsers.Fill(usersDataSet, "Users"); daSelectUsers.Update(usersDataSet, "Users"); } }
Thank you for contacting Infragistics.
If you are using the standard DataSet/DataAdapter, then these objects have support for all of the interaction with the back end (the database). The grid only deals with the local data source, it has no connection beyond that. So you might want to check out Microsoft's documentation on the DataAdapter.Update method.
Let me know if you have any questions.