How to implement INotifyPropertyChanged like interface in my WPF project?

don bradman 621 Reputation points
2022-02-12T01:37:41.693+00:00

I have a WPF app where the data source of a datagrid are filtered data of a excel worksheet. The worksheet looks something like this :

173696-image.png

I'm loading the filtered data from that sheet to a datagrid using the below code on window load event :

			DataTable dt = new DataTable();  
  
			dt.Columns.Add("Party");  
			dt.Columns.Add("Bill Number");  
			dt.Columns.Add("Bill Date");  
			dt.Columns.Add("Amount");  
  
			ExcelPackage.LicenseContext =LicenseContext.NonCommercial;  
  
			using (ExcelPackage excelPackage = new ExcelPackage(file_Bills))  
			{  
  
				ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.First();  
  
				//loop all rows  
				for (int i = worksheet.Dimension.Start.Row+1; i <= worksheet.Dimension.End.Row; i++)  
				{  
					DataRow dr = dt.NewRow();  
					bool addRow = false;  
  
					for (int j = worksheet.Dimension.Start.Column; j <= 6; j++)  
					{  
                        //filtering criteria for showing in data grid  
						if (DateTime.Parse(worksheet.Cells[i, 5].Text) >= DateTime.Today && DateTime.Parse(worksheet.Cells[i, 5].Text) <= DateTime.Today.AddDays(10) && string.IsNullOrEmpty(worksheet.Cells[i, 6].Text))  
						{  
							addRow =true;  
						}  
  
					}  
					if (addRow)  
					{  
						dt.Rows.Add(worksheet.Cells[i, 1].Text, worksheet.Cells[i, 2].Text, worksheet.Cells[i, 3].Text, worksheet.Cells[i, 4].Text);  
						dt.AcceptChanges();  
					}  
  
				}  
  
			}  
			pendingBillsGrid.ItemsSource=dt.DefaultView;  
			  
			int pendingBills=pendingBillsGrid.Items.Count;  
			if (pendingBills > 0)  
			{  
				txtBlk.Inlines.Clear();  
				txtBlk.Inlines.Add(new System.Windows.Documents.Run("Bills pending for Processing : ") { Foreground = System.Windows.Media.Brushes.Aquamarine });  
				txtBlk.Inlines.Add(new System.Windows.Documents.Run(pendingBills.ToString()) { Foreground = System.Windows.Media.Brushes.Red, FontSize = 14 });  
			}  
			else  
			{  
				txtBlk.Inlines.Clear();  
				txtBlk.Inlines.Add(new System.Windows.Documents.Run("No Bill/Bills pending for Processing...") { Foreground = System.Windows.Media.Brushes.Aquamarine });  
			}  

Now I have a button click event which modifies the above excel file, saves it & then closes the workbook which in turn could change the rows that are shown in the datagrid but in order to do that I have to either manually close the app and then run it again or do this programmatically. I was wondering whether this can be done more efficiently than that like using INotifyPropertyChanged like interface where the datagrid data automatically gets updated when the button click event finishes.

Can anyone show me how to do that ?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,781 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,962 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
812 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.