Why is data not showing in datagrid in WPF c# app?

don bradman 621 Reputation points

I have a Datagrid (named 'dg') & a Combobox (named 'cmb2') in my c# WPF app. What I'm trying to do is when I select an item from the combobox the app opens a excel file (*.xlsx) in read-only mode and filters the excel data by matching the text of the combobox with first column of the excel file and displays the filtered excel file data in the datagrid.

So, I have implemented a SelectionChanged event in cmb2 as follows :

        private void cmb2_SelectionChanged(object sender, SelectionChangedEventArgs e)  
			int rowno = 0;  
			string kword = cmb2.Text;  
			using (ExcelPackage package = new ExcelPackage(new System.IO.FileInfo(file_Bills), false))  
				ExcelWorksheet workSheet = package.Workbook.Worksheets[0];  
				for (int i = 2; i <= workSheet.Dimension.End.Row; i++)  
					if (!string.IsNullOrEmpty(workSheet.Cells["A"+i].Text))  
						rowno =i;  
				DataTable dtTemp = new DataTable();  
				dtTemp.Columns.Add("Bill No.");  
				dtTemp.Columns.Add("Bill Date");  
				dtTemp.Columns.Add("Due Date");  
				dtTemp.Columns.Add("Payment Released on");  
				//DataRow drAddItem;  
				for (int row = 2; row <= rowno; row++)  
					if (workSheet.Cells[row, 1].Text == kword)  
						FilterDGV(dtTemp, workSheet, row);  

Using the below helper method :

		public static void FilterDGV(DataTable tbl, ExcelWorksheet sht, int rowIndex)  
			DataRow drAddItem;  
			var columnMapping = new[]  
				"Bill No.",  
				"Bill Date",  
				"Due Date",  
				"Payment Released on"  
			drAddItem = tbl.NewRow();  
			var columnIterator = columnMapping.GetEnumerator();  
			for (int column = 1; column < 9; column++)  
				if (column == 7) continue;  
				object columnValue = sht.Cells[rowIndex, column].Text;  
				drAddItem[columnIterator.Current.ToString()] = columnValue.ToString();  

But when I select an item from the combobox, only the headers appear and not the actual data.



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,686 questions
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,375 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 49,246 Reputation points

    Set a breakpoint on the line dg.ItemsSource=dtTemp.DefaultView; and run your code. When you hit the breakpoint look at the value of dtTemp. Does it have any rows in it? Now look at the results of DefaultView. Do you lose the rows? If so then the problem is with your default view. If both values have rows then the issue is with setting the grid.

    If the grid was populated properly before then we can assume it is a data issue. If you haven't gotten the grid to populate at all yet then it could also be a configuration issue with the grid. Perhaps your template is wrong. It would be hard to say at this point.

    1 person found this answer helpful.