[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 459
  • Last Modified:

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.
0
wiswalld
Asked:
wiswalld
  • 6
  • 6
1 Solution
 
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
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now