locked
GridView dates RRS feed

  • Question

  • User-49671077 posted

    The following code popup dates for selected month in columns, I want these dates popup in gridview header or first row each columns.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            Dim dtDates As DataTable = New DataTable
            dtDates.Columns.Add("Date")
            
            If (DateTime.Now.Day < 31) Then
                Dim dtStart As DateTime = New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1)
                Dim i As Integer = 0
                Do While (i < 31)
                    dtDates.Rows.Add(New Object() {dtStart.ToString("dd-MM")})
                    ' with format
                    dtStart = dtStart.AddDays(1)
                    'dtDates.Rows.Add(new Object[] { dtStart }); // without format
                    i = (i + 1)
                Loop
                
            Else
                Dim dtStart As DateTime = New DateTime(DateTime.Now.Year, DateTime.Now.Month, 16)
                Dim i As Integer = 0
                Do While (i < 31)
                    dtDates.Rows.Add(New Object() {dtStart.ToString("dd-MM")})
                    ' with format
                    dtStart = dtStart.AddDays(1)
                    'dtDates.Rows.Add(new Object[] { dtStart }); // without format
                    i = (i + 1)
                Loop
                
            End If
            
            Me.GridView1.DataSource = dtDates
            Me.GridView1.DataBind
        End Sub

    Can someone help please 
     

    Saturday, February 22, 2020 10:16 PM

Answers

  • User1535942433 posted

    Hi kafsar,

    Accroding to your codes,I suggest you could create a new boundfield and loop through the dtDates and add in the field headertext.

    More details,you could refer to below codes:

    ASPX:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                </asp:GridView>

    Code-behind:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim dtDates As DataTable = New DataTable()
        dtDates.Columns.Add("Date")
    
        If (DateTime.Now.Day < 31) Then
            Dim dtStart As DateTime = New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1)
            Dim i As Integer = 0
    
            While (i < 31)
                dtDates.Rows.Add(New Object() {dtStart.ToString("dd-MM")})
                dtStart = dtStart.AddDays(1)
                i = (i + 1)
            End While
        Else
            Dim dtStart As DateTime = New DateTime(DateTime.Now.Year, DateTime.Now.Month, 16)
            Dim i As Integer = 0
    
            While (i < 31)
                dtDates.Rows.Add(New Object() {dtStart.ToString("dd-MM")})
                dtStart = dtStart.AddDays(1)
                i = (i + 1)
            End While
        End If
    
        For j As Integer = 0 To dtDates.Rows.Count - 1 - 1
            Dim col As BoundField = New BoundField()
            col.HeaderText = dtDates.Rows(j)(0).ToString()
            GridView1.Columns.Add(col)
        Next
    
        Me.GridView1.DataSource = dtDates
        Me.GridView1.DataBind()
    End Sub

    Result:

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 24, 2020 5:38 AM

All replies

  • User-1716253493 posted

    To set header  text

    GridView1.Columns(0).HeaderText = "1"

    Loop the data to set the header

    Sunday, February 23, 2020 2:03 AM
  • User1535942433 posted

    Hi kafsar,

    Accroding to your codes,I suggest you could create a new boundfield and loop through the dtDates and add in the field headertext.

    More details,you could refer to below codes:

    ASPX:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                </asp:GridView>

    Code-behind:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim dtDates As DataTable = New DataTable()
        dtDates.Columns.Add("Date")
    
        If (DateTime.Now.Day < 31) Then
            Dim dtStart As DateTime = New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1)
            Dim i As Integer = 0
    
            While (i < 31)
                dtDates.Rows.Add(New Object() {dtStart.ToString("dd-MM")})
                dtStart = dtStart.AddDays(1)
                i = (i + 1)
            End While
        Else
            Dim dtStart As DateTime = New DateTime(DateTime.Now.Year, DateTime.Now.Month, 16)
            Dim i As Integer = 0
    
            While (i < 31)
                dtDates.Rows.Add(New Object() {dtStart.ToString("dd-MM")})
                dtStart = dtStart.AddDays(1)
                i = (i + 1)
            End While
        End If
    
        For j As Integer = 0 To dtDates.Rows.Count - 1 - 1
            Dim col As BoundField = New BoundField()
            col.HeaderText = dtDates.Rows(j)(0).ToString()
            GridView1.Columns.Add(col)
        Next
    
        Me.GridView1.DataSource = dtDates
        Me.GridView1.DataBind()
    End Sub

    Result:

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 24, 2020 5:38 AM