Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please help me to add dates column name in a grid view.
I know i have to put my code in loop to generate this.
kindly tell me how to loop addition of Grid view columns.

I am getting error like same column name is already exist.

What I have tried:

//days is start of month(2018/05/01)

for(i=1;i<days,i++)
{
datacolumn dt= new datacolumn(d.dateofweek()+d.shortdatestring())
dt.column.add(dt);
d=d.adddays(1);
}
Posted
Updated 26-Jul-18 12:15pm
v2

Try below code for your problem.

This will add date columns to datagridview.

//Get current date
            DateTime dt = DateTime.Now; 
            //Get first date
            var startDate = new DateTime(dt.Year, dt.Month, 1);
            //Get tota no of days
            int days = DateTime.DaysInMonth(dt.Year, dt.Month);

            //Loop on days
            for (int i = 0; i < days; i++)
            {        
                dataGridView1.Columns.Add("clm_" + startDate.ToShortDateString(), startDate.ToString("yyyy/MM/dd"));
                startDate= startDate.AddDays(1);
            }
 
Share this answer
 
Comments
Member 13858616 5-Jun-18 23:00pm    
I am getting error in add..No overload for Add takes 2 values
Member 13858616 5-Jun-18 23:07pm    
Only showing error in add
Member 13858616 6-Jun-18 0:17am    
Sir.. I got the syntax now properly... Thanks a lot for helping me..
Solved
Sir.. I got the syntax now properly... Thanks a lot for helping me..
 
Share this answer
 
Comments
Richard Deeming 6-Jun-18 10:40am    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution.

DO NOT post your reply as a new "solution".
Member 13858616 26-Jul-18 18:14pm    
Sure.
I found answer.. thank you sir
 
Share this answer
 
Comments
CHill60 27-Jul-18 3:50am    
You have already been told not to post replies as "solutions". You are also very rude in accepting your own non-solution instead of Chander's when he clearly helped you solve the problem

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