I can add data to database but there result of it I can only see after restart of my program. How to make it possible to add data and then(immediately. with waiting) see it in another window with datagrid?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I am assuming you are binding the DataGrids Items property to your data, or something like that… Have you implemented INotifyPropertyChanged on whatever class your are holding your data in? This also supposes that you have some way of telling your DataSource to refresh it’s data. If you are issuing SQL commands that insert data, and you have a TableAdapter or something that is storing your local copy, but you don’t tell the TableAdapter to refresh from the database, you will never see your changes.
This works best when using MVVM, but you can make it work otherwise. Essentially, you Add the PropertyChanged event and a handler to your class. I usually put a call to the handler in my property setter as well as any time I explicitly make changes to the properties value.
This tells the UI that the data has changed in some way, and to refresh the Views representation of it.
VB Example
C# Example
ObservableCollection
I am also a huge fan of ObservableCollection. If you have the means to use it, I suggest it. It is IEnumerable and has INotifyCollectionChanged implemented on it already.
Clarification of Databinding
Lets Say you have a class (call it MyData) which you are using to contain your data, in this case a collection of Person objects (ObservableCollection People). Your XAML might look like this. This assumes that your MyData class has a default constructor for brevity.
In this way the DataContext is describi9ng the location of the data, and the ItemsSource is telling what the control should do with it.
Code Project article dealing with databinding DataGrid to TableAdapter data.