Click here to Skip to main content
15,922,584 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have A DataGridView in C# WinForm
In DataGrid i Have 4 Columns and 12 Rows
Column 1 = Sr No
Column 2 = Name
Column 3 = Date
Column 4 = Amount

I Auto generate Row By TextBox Value
but i also need Data Column fill all cell by +1 month

What I have tried:

I need [Date] Columns in my datagridview AutoFill by loop add +1 Month in each cell
like
20/01/2020
20/02/2020
20/03/2020
20/04/2020
.
.
.
20/12/2020
Posted
Updated 13-Mar-20 6:37am
v2
Comments
phil.o 2-Mar-20 9:18am    
And what have you tried?
Please improve your question and show the code which is actually responsible for filling the grid.
[no name] 2-Mar-20 9:24am    
There is a method for DateTime to add N month, see here: DateTime.AddMonths(Int32) Method (System) | Microsoft Docs[^]
Maciej Los 2-Mar-20 9:27am    
Sounds like an answer, Bruno.
[no name] 2-Mar-20 9:41am    
Thank you Maciej, I will try my luck :)

you can try this

loop for month you can use this

on button_click event or whatever.

datetime dt=convert.todatetime("20/01/2020");

for(int i=1;i<=12;i++) /////(i till 12 bcoz you have add 12 rows)
{

int n=datagridview1.rows.add();


datagridview1.rows[n].cell[0].value= dt.toshortdatestring();

dt=dt.addmonth(1);

}
 
Share this answer
 
In case I understand your request correctly, you simply search for a way to 'increment' months for a given DateTime?

If yes have a look here, looks pretty easy with that:
DateTime.AddMonths(Int32) Method (System) | Microsoft Docs[^]

I hope it helps.
 
Share this answer
 
Comments
Maciej Los 2-Mar-20 10:25am    
5ed!
[no name] 2-Mar-20 10:28am    
Thank you very much Maciej, now because of you I'm over 100K :)
Maciej Los 2-Mar-20 11:39am    
Well, i remember this feeling, when i've reached first 100K...
Congrats!
Amar chand123 3-Mar-20 4:37am    
but how i apply this code in my Datagridview
For SrNo. "increment" i use this Code:

private void LoadSr()
{
int i = 1;
foreach(DataGridViewRow row in dataGridView1.Rows)
{
row.Cells["SrNo"].Value = i; i++;
}
}

Can You Modify this Code for my Date Column Because i am Beginner in Programming
so i not Modify code, So if You Help Me for Change in Code, it help me a lot,
Thank You
Member 14772490 13-Mar-20 23:34pm    
private void LoadSr()
{
int i = 1;
foreach(DataGridViewRow row in dataGridView1.Rows)
{
row.Cells["SrNo"].Value = i; i++;
}
}

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