I'm trying to export data from a .xlsx file to a Listview control where the date of the "Due Date" column falls between today's date and the next 10 days from today and below is my code (I'm using EPPlus library for excel file stuff):
using (ExcelPackage package = new ExcelPackage(new FileInfo(filename), false))
{
ExcelWorksheet mainSheet = package.Workbook.Worksheets.First();
listView1.Columns.Add("OrderCode");
listView1.Columns.Add("Invoice");
listView1.Columns.Add("Items");
listView1.Columns.Add("NetAmount");
listView1.Columns.Add("Due Date");
for (int row = 2; row <= 100; row++)
{
for (int col = 1; col < 6; col++)
{
if (DateTime.Parse(mainSheet.Cells[row, 5].Text) >= DateTime.Today && DateTime.Parse(mainSheet.Cells[row, 5].Text) <= DateTime.Today.AddDays(10))
{
listView1.Items.Add(mainSheet.Cells[row, col].Text);
}
}
}
}
But all the data are clumping in the first column of the Listview, even though I've set View property to Details. Also, the data filtering by dates falling in the above mentioned range is not working as intended.
How to fix this?
Nitin SontakkePosted Dec 27, 2021, 6:20 AM
I may not be able to give you the code, but I can give you a hint and you can take it from there if you are happy with it.
Here is what you could / should / would do.
Declare a class with all the properties that you want to have it bound to ListView, say for example, Order.
Then inside the for loop, for each row create an instance and initiaze properties of that class.
Add it to the List orders collection.
Then assign orders to the DataSource property of the ListView.
If possible separate each part of code. For example, write a separate method which would read the Excel work-sheet and populate list, etc.
Hope it helps!