We help IT Professionals succeed at work.

Add column to datagridview and add one day

wiswalld
wiswalld asked
on
Medium Priority
471 Views
Last Modified: 2012-08-14
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.
Comment
Watch Question

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
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

Author

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

Author

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).
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
You can adjust the range of dates in the dateStart and dateEnd variable declarations.

Author

Commented:
OK

I had two columns there but this over wrote them. I want to keep those columns (0 and 1)
       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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
How to stop someone from hitting the button twice.
What button?

Author

Commented:
I guess this would not need a button. Originally I was testing this with a button.

Author

Commented:
Works good. I am going to post another question as a follow up to this one.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.