Click here to Skip to main content
16,022,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to check the expiration date that display in my datagridview. I have a code but not work for me.

What I have tried:

private void NotificationForForm()
        {

            foreach (DataGridViewRow drow in dataGridView2.Rows)
            {
                DateTime expdate = DateTime.Parse(drow.Cells[12].Value.ToString());

                TimeSpan tmspn = expdate - DateTime.Now.Date;
                if (tmspn.Days < 1)
                {
                    notification("WARNING", "Attention! A vehicle schedule for \nmaintenance is detected. \nPlease check your car in your garage.");
                }
            }
}


I want to show notification if they have expired date.
Posted

We can't help you fix this: it needs your data as well as your code to work out what is wrong.

However, there are a few general things you can try checking with the debugger:
1) Check that you are reading the right cell: using numeric indexes for column access is prone to failure, particularly if you use a DB and SELECT * FROM ... type SQL commands.
2) Check that the method is called - put a breakpoint at the top of the method and check your code is actually executing.
3) Check that something further up the chain isn't "swallowing" exceptions that you need to see: if the date format isn't understandable by the Parse call, it will throw an exception which something else may be catching. Single stepping in the debugger is the simplest way to track that.
4) Check what the original datatype is: whenever I see a Parse call containing a ToString call I start thinking "someone isn't sure what to do here and was guessing". If you are reading this from a DB, then the DB column should be DateTime anyway, so a simple cast is all you should need. If it isn't, then the DB design is poor and needs fixing first.
5) Check that the notification method does what you expected - again, single stepping is the best way to find out.
 
Share this answer
 
Your notification is:
notification("WARNING", "Attention! A vehicle schedule for \nmaintenance is detected. \nPlease check your car in your garage.");

So, why do you try to compare datagridviewcell.value to current date?
Your DataGridView object have to be bind to List<Car>. A Car object have to be your own class, ie.:
C#
void Main()
{
	List<Car> cars = new List<Car>()
	{
		new Car("Skoda", new DateTime(2024, 05, 31)),
		new Car("Mercedes", new DateTime(2024, 06, 19)),
		new Car("Fiat", new DateTime(2024, 06, 26)),
		new Car("Volkswagen", new DateTime(2024, 07, 01))
	};
	
	foreach(Car c in cars)
	{
		if(c.DaysToMaintenance == 1)
			Console.WriteLine($"{c.CarName} => tommorow is maintenance day!");
	}
	
}

// Definition of class is here:
internal class Car
{
	private string sName = string.Empty;
	private DateTime md = DateTime.MinValue;
	private int dtomd = 0;
	
	public Car(string _Name, DateTime _MaintenanceDate)
	{
		sName = _Name;
		md = _MaintenanceDate;
		dtomd = (md - DateTime.Today).Days;
	}
	
	public string CarName
	{
		get => sName;
		set => sName = value;
	}
	
	public DateTime MaintenanceDate
	{
		get => md;
		set => md = value;
	}
	
	public int DaysToMaintenance
	{
		get => dtomd;
	}

}



More about data binding you'll find here:
Bind Objects to DataGridView Controls - Windows Forms .NET Framework | Microsoft Learn[^]
Bind data to DataGridView Control - Windows Forms .NET Framework | Microsoft Learn[^]
 
Share this answer
 
I would look at this very differently.

I am guessing that you store your data in a database. You have a Winforms app with a DataGrid that is a view of data in your database. This view allows you to also allow CRUD (Create/Add, Read, Update/make changes, and Delete). This should be it's sole purpose.

You have the need to do operations on your data that is not user tasks. One of these tasks is to check your database to see if a condition, or multiple conditions, has occurred. If so, then you need to notify the user, a customer, or some other stakeholder. This notification could be in the form of a popup window, a control on a form in the app, an email notification, etc.

Notice, as I describe each function of your app, it is showing how the design of the app should be done. In this case, of your notification, it is not part of the data entry (DataGrid). It is saying that it is a separate process. So, you should be using a Background Task, separate to the DataGrid, that periodically looks at the database.

You can read more about Background Tasks for Dot Net Framework here: How to: Run an Operation in the Background - Windows Forms .NET Framework | Microsoft Learn[^] or if you're using Dot Net core: Create Windows Service using BackgroundService - .NET | Microsoft Learn[^]

Hope this helps. 😊
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900