Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i Have Same Columns in My DataGridView in C# WinForm

LastDate TodayDate RemainingDay
18/05/2020 01/05/2020 17


I Need in my DataGridView (LastDate) and (TodayDate) Columns Values Minus Automatic and Show in My (RemainingDay) Columns

What I have tried:

i Try Same Method But Not Work Properly

so Please Help me
Posted
Updated 1-May-20 22:43pm

Perhaps you could use the DateTime.Subtract()[^] method to populate that column.

Reference:
DateTime.Subtract Method (System) | Microsoft Docs[^]
 
Share this answer
 
C#
int remainingDays = (lastDate - todayDate).Days;
 
Share this answer
 
Comments
MadMyche 1-May-20 8:44am    
+5 Hopefully these columns truly are dates and not strings
phil.o 1-May-20 8:49am    
Thanks :) I wondered the same, but we can just ammend the answer if strings were used instead of proper datatype.
Amar chand123 1-May-20 9:06am    
private void LoadSr()
{

foreach (DataGridViewRow row in dataGridView2.Rows)
{
DateTime dt = Convert.ToDateTime(dataGridView2.CurrentRow.Cells["LasttDate"].Value);
DateTime dt1 = Convert.ToDateTime(dataGridView2.CurrentRow.Cells["TodayDate"].Value);
int d12 = (dt - dt1).Days;

row.Cells["DueDay"].Value = d12;

}
}
sir, i Try This Code But Show Error i am is new in Programming so can you help

An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll

Additional information: Object cannot be cast from DBNull to other types.
phil.o 1-May-20 9:22am    
It means that one of the column has not been provided any value, and thus is null.
As a side note, please do NOT use Convert class to handle valuetype's parsing; use DateTime.TryParse() method instead.
if (!DateTime.TryParse(dataGridView2.CurrentRow.Cells["LasttDate"].Value, out DateTime lastDate))
{
   // Problem: lastDate could not be properly parsed. You have to decide what to do in that case.
   // ...
}
if (!DateTime.TryParse(dataGridView2.CurrentRow.Cells["TodayDate"].Value, out DateTime todayDate))
{
   // Problem: todayDate could not be properly parsed. You have to decide what to do in that case.
   // ...
}
int remaining = (lastDate - todayDate).Days;
IFf this is just for display purposes, then the easiest way is to handle the CellPainti8ng event for the DGV, check if it's the "difference column", and if it is, calculate it and show the value. I do something similar with couling DGV cells to indicate anomalous values: Colouring DataGridView Cells According to their Content in WinForms[^] - it's should be pretty easy to do what you wanted from that.

If you want it persisted back to your DB, then you would need to work on the data source, not the DGV .
 
Share this answer
 
Maybe you can use the difference between a date in sql query
SELECT LastDate ,TodayDate ,DATEDIFF(day, LastDate, TodayDate) AS RemainingDay
 
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