Reputation: 50
I am building a sale application on wpf and I have a datagrid that contains sales data and at each time I execute an insert, delete or update query, I have to perform the display query shown below, is there any way I can bind the datagrid directly to the SQL Server table, so that it's updated automatically when the SQL Server table is changed?
Code behind:
connection.open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT*FROM Comptoir", connection);
tble.Clear();
adapter.Fill(tble);
ComptoirGrid.ItemsSource = tble.DefaultView;
connection.close();
Upvotes: 0
Views: 5671
Reputation: 50
I finanlly found the solution for it, and it's really this simple, adding a datasource to the database and then right click on the table you want and there you have a datagrid binding template already set, then you drag it to the location you want in the window, you will see the binding code on xaml automatically generated, I hope this will be helpfull.
Upvotes: 0
Reputation: 27
try this
using (SqlConnection con = new SqlConnection(connection_string))
{
con.Open();
// 2
// Create new DataAdapter
using (SqlDataAdapter a = new SqlDataAdapter(
"SELECT * FROM Contacts", con))
{
// 3
// Use DataAdapter to fill DataTable
DataTable t = new DataTable();
a.Fill(t);
// 4
// Render data onto the screen
Data_table.DataSource = t;
}
}
put this whenever you take action to your SQL server database like when you edit your data pressed on save button or update put this code, reply what will happen with you. good luck bro 👍
Upvotes: 1