Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Try a Code for Automatic Add Months Loop in my DataGridView in C#
Code Work Fine when i use Date 1 to 28 like (15/12/2020, 25/01/2020)
But I have Problem when i use 29 to 30 or 31 i have a Problem
For Example
My Code Result:---
Date1 15/07/2020(Work Fine)   Date2 30/07/2020(Problem)    Date3 31/07/2020(Problem)
  Result(Date1)   ......       Result(Date2)..........            Result (Date3)
15/08/2020      ........       30/08/2020 ............                    31/08/2020
15/09/2020      ........       30/09/2020 ............                    30/09/2020
15/10/2020      ........       30/10/2020 ............                    30/10/2020(P)
15/11/2020      ........       30/11/2020 ............                    30/11/2020
15/12/2020      ........       30/12/2020 ............                    30/12/2020(P)
15/01/2021      ........       30/01/2021 ............                    30/01/2021(P)
15/02/2021      ........       28/02/2021 ............                    28/02/2021(P)
15/03/2021      ........       28/03/2021(P)........                  28/03/2021(P) 
15/04/2021      ........       28/04/2021(P)........                  28/03/2021(P)
15/05/2021      ........       28/05/2021(P)........                  28/03/2021(P)
15/06/2021      ........       28/06/2021(P)........                  28/03/2021(P)
15/07/2021      ........       28/07/2021(P)........                  28/03/2021(P)


What I have tried:

My Code is
C#
private void LoadDate()
{
    DateTime dt=Convert.ToDateTime(textBoxLoanDate.Text);
    for (int i = 0; i < 1; i++)
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        dt = dt.AddMonths(1);
        row.Cells["Date"].Value = dt.ToString("dd/MM/yyyy");
        
    }
}

Please Help me Solve this Problem
Posted
Updated 3-Mar-20 23:51pm
v2
Comments
OriginalGriff 4-Mar-20 3:24am    
What problem?
What does it do that you didn't expect, or not do that you did?
Are there any error messages, and if so, what are they, and where do they happen?

Use the "Improve question" widget to edit your question and provide better information.
Amar chand123 4-Mar-20 5:34am    
there is no Error Message Problem is When i use loop Month then as you see when i use (Date1) Loop Code Work is Fine, but when i use ( Date2 or 3 ) my loop code not work Properly ( if i use date 31/12/2020 then after 28th February my code change date 28/03/2021, 28/04/2021, 28/05/2021 but i need this Result 31/03/2021, 30/04/2021, 31/05/2021

The problem is that you are using the previous date and then adding one month to it. If you instead take the start date and always add a number of months to it the sequence will be correct:
C#
var start = new DateTime(2020, 8, 30);
for (int i = 0; i < 10; i++)
{
	Console.WriteLine(start.AddMonths(i).ToString("dd/MM/yyyy"));
}

The result:
30/08/2020
30/09/2020
30/10/2020
30/11/2020
30/12/2020
30/01/2021
28/02/2021
30/03/2021
30/04/2021
30/05/2021

In your code:
C#
private void LoadDate()
{
    DateTime dt=Convert.ToDateTime(textBoxLoanDate.Text);
    var monthCounter = 1;
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        row.Cells["Date"].Value = dt.AddMonths(monthCounter).ToString("dd/MM/yyyy");
        monthCounter++;       
    }
}
 
Share this answer
 
v3
Comments
Amar chand123 4-Mar-20 5:23am    
Sir Tomas Takac
how to use this code in my Code, can you modify my code because i am is beginner in Programming, thank you
Tomas Takac 4-Mar-20 5:46am    
I updated my answer with your code, how it should look like. But you need to understand the concepts and be able to apply them in different situation.
Amar chand123 4-Mar-20 6:10am    
Sir, if i use Date 31/08/2020 in your Code then i Result Show---
30/09/2020
30/11/2020
28/02/2021
28/06/2021
28/11/2021
28/05/2022
28/12/2022
28/08/2023
Tomas Takac 4-Mar-20 6:53am    
My bad, I fixed the bug, check again.
Amar chand123 4-Mar-20 7:42am    
Thank you for help Sir
this code work Fine and sir can you explain me what Problem in your previous Code for my Future reference
Quote:
there is no Error Message Problem is When i use loop Month then as you see when i use (Date1) Loop Code Work is Fine, but when i use ( Date2 or 3 ) my loop code not work Properly ( if i use date 31/12/2020 then after 28th February my code change date 28/03/2021, 28/04/2021, 28/05/2021 but i need this Result 31/03/2021, 30/04/2021, 31/05/2021

The safest way to do that is take the first of month you want the last day of, add one month, and subtract one day. That always gives you the last day of that month, regardless of how many days it contains and what your original start date was.
This may help: DateTime Extensions to Make Some Simple Tasks a Little More Readable[^]
 
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