Add column to datagridview and add one day

I want to add a column to a datagridview. I want the first added column header text to be the 1st of the year and every added column header text after that to be plus one day.

I have started with this

Me.DataGridView1.Columns.Add("Date1", "Date1")
Me.DataGridView1.Columns(1).HeaderText = DateTime.Now.ToString("MM/dd/yyyy")

I also want to name each column for the date entered in the header text.
LVL 13
wiswalldAsked:
Who is Participating?
 
AgentSmith007Connect With a Mentor Commented:
       Dim dateStart As New Date(2009, 1, 1)
        Dim dateEnd As New Date(2009, 12, 31)
        Dim dateCurrent As Date = dateStart
        Dim iIndex As Integer = Me.DataGridView1.Columns.Count
        While dateCurrent <= dateEnd
            Me.DataGridView1.Columns.Add("Date" & iIndex, "Date" & iIndex)
            Me.DataGridView1.Columns(iIndex).HeaderText = dateCurrent.ToString("MM/dd/yyyy")
            dateCurrent = dateCurrent.AddDays(1)
            iIndex += 1
        End While
0
 
AgentSmith007Commented:
Something like this?

        Dim dateStart As New Date(2009, 1, 1)
        Dim dateEnd As New Date(2009, 12, 31)
        Dim dateCurrent As Date = dateStart
        Dim iIndex As Integer = 0
        While dateCurrent <= dateEnd
            Me.DataGridView1.Columns.Add("Date" & iIndex, "Date" & iIndex)
            Me.DataGridView1.Columns(1).HeaderText = dateCurrent.ToString("MM/dd/yyyy")
            dateCurrent.AddDays(1)
            iIndex += 1
        End While
0
 
AgentSmith007Commented:
Sorry, would need to change that Colums(1) reference:

        Dim dateStart As New Date(2009, 1, 1)
        Dim dateEnd As New Date(2009, 12, 31)
        Dim dateCurrent As Date = dateStart
        Dim iIndex As Integer = 0
        While dateCurrent <= dateEnd
            Me.DataGridView1.Columns.Add("Date" & iIndex, "Date" & iIndex)
            Me.DataGridView1.Columns(iIndex).HeaderText = dateCurrent.ToString("MM/dd/yyyy")
            dateCurrent.AddDays(1)
            iIndex += 1
        End While
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
wiswalldAuthor Commented:
ERROR: Sum of the columns' FillWeight values cannot exceed 65535.

Dim dateStart As New Date(2009, 1, 1)
        Dim dateEnd As New Date(2009, 12, 31)
        Dim dateCurrent As Date = dateStart
        Dim iIndex As Integer = 0
        While dateCurrent <= dateEnd
            Me.DataGridView1.Columns.Add("Date" & iIndex, "Date" & iIndex)  <<<<<<<<<<HERE>>>>>
            Me.DataGridView1.Columns(iIndex).HeaderText = dateCurrent.ToString("MM/dd/yyyy")
            dateCurrent.AddDays(1)
            iIndex += 1
        End While
0
 
wiswalldAuthor Commented:
Also

I don't know if I just want to add a day for every day in the year. I want to add them as I go (this may be more than one year).
0
 
AgentSmith007Commented:
Sorry about that, looks like the dateCurrent wasn't getting incremented so it was stuck in an infinite loop... this should fix it:

Dim dateStart As New Date(2009, 1, 1)
        Dim dateEnd As New Date(2009, 12, 31)
        Dim dateCurrent As Date = dateStart
        Dim iIndex As Integer = 0
        While dateCurrent <= dateEnd
            Me.DataGridView1.Columns.Add("Date" & iIndex, "Date" & iIndex)
            Me.DataGridView1.Columns(iIndex).HeaderText = dateCurrent.ToString("MM/dd/yyyy")
            dateCurrent = dateCurrent.AddDays(1)
            iIndex += 1
        End While
0
 
AgentSmith007Commented:
You can adjust the range of dates in the dateStart and dateEnd variable declarations.
0
 
wiswalldAuthor Commented:
OK

I had two columns there but this over wrote them. I want to keep those columns (0 and 1)
0
 
wiswalldAuthor Commented:
How to stop someone from hitting the button twice.
0
 
AgentSmith007Commented:
What button?
0
 
wiswalldAuthor Commented:
I guess this would not need a button. Originally I was testing this with a button.

0
 
wiswalldAuthor Commented:
Works good. I am going to post another question as a follow up to this one.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.