Click here to Skip to main content
14,218,975 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi all... i have problem when i try make dynamic add datagridviewcolumns in visual basic language. so columns will add when start date or end date selected value change.

What I have tried:

my code is :


Sub LoadFormatgvPanelRoomRack()

        gvPanelRoomRack.Columns.Clear()
        gvPanelRoomRack.Rows.Clear()

        Dim pTimeCI As DateTime
        Dim pTimeCO As DateTime

        Try
            con.Close()
            con.Open()
            strSql = "SELECT tp_tblsttimerangesetting.KdTRS," & _
                            "tp_tblsttimerangesetting.CITime," & _
                            "tp_tblsttimerangesetting.COTime," & _
                            "tp_tblsttimerangesetting.KdActiveStatus" & _
                     " FROM tp_tblsttimerangesetting" & _
                     " WHERE tp_tblsttimerangesetting.KdActiveStatus=1"
            cmd = New MySqlCommand(strSql, con)
            dr = cmd.ExecuteReader
            dr.Read()
            If dr.HasRows Then
                pTimeCI = dr.Item(1).ToString
                pTimeCO = dr.Item(2).ToString
            End If
            dr.Close()
            dr.Dispose()
        Catch ex As Exception
            Beep()
            'MessageBox.Show("Terjadi kesalahan dalam pengambilan data room", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
            MsgBox("Error LoadDataOccupancy: " & ex.Message)
        Finally
            con.Close()
            con.Dispose()
        End Try

        pTimeCI = pTimeCI.ToLongTimeString
        pTimeCO = pTimeCO.ToLongTimeString


        Dim pDateCI As Date
        Dim pDateCO As Date

        If dtpStart.Text = "" Then
            pDateCI = Now
        Else
            pDateCI = dtpStart.Text
        End If
        If dtpEnd.Text = "" Then
            pDateCO = Now.AddDays(3)
        Else
            pDateCO = dtpEnd.Text
        End If

        Dim selisih As Integer
        selisih = DateDiff(DateInterval.Day, pDateCI, pDateCO)
        TextBox2.Text = selisih + 1

        With gvPanelRoomRack
            .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
            .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
            .ColumnHeadersHeight = 40
            .RowHeadersWidth = 41

            Dim colRoomNumber As New DataGridViewButtonColumn()
            With colRoomNumber
                .Name = "RoomNumber"
                .HeaderText = "No."
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .SortMode = DataGridViewColumnSortMode.NotSortable
                .FlatStyle = FlatStyle.Standard
            End With
            .Columns.Add(colRoomNumber)

            Dim colRoomType As New DataGridViewButtonColumn()
            With colRoomType
                .Name = "RoomType"
                .HeaderText = "R. Type"
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .SortMode = DataGridViewColumnSortMode.NotSortable
                .FlatStyle = FlatStyle.Standard
            End With
            .Columns.Add(colRoomType)

            Dim colBedType As New DataGridViewButtonColumn()
            With colBedType
                .Name = "BedType"
                .HeaderText = "Bed"
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .SortMode = DataGridViewColumnSortMode.NotSortable
                .FlatStyle = FlatStyle.Standard
            End With
            .Columns.Add(colBedType)

            Dim colToday As New DataGridViewButtonColumn()
            With colToday
                .Name = "Today"
                .HeaderText = "Today"
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .SortMode = DataGridViewColumnSortMode.NotSortable
                .FlatStyle = FlatStyle.Standard
            End With
            .Columns.Add(colToday)

            Dim colCIDate As New DataGridViewTextBoxColumn()
            With colCIDate
                .Name = "CIDate"
                .HeaderText = (Format(pDateCI, "yyyy/MM/dd") & " " & pTimeCI)
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .SortMode = DataGridViewColumnSortMode.NotSortable
            End With
            .Columns.Add(colCIDate)


            Dim colCODate As New DataGridViewTextBoxColumn()
            With colCODate
                .Name = "CODate"
                .HeaderText = (Format(pDateCI.AddDays(1), "yyyy/MM/dd") & " " & pTimeCO)
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .SortMode = DataGridViewColumnSortMode.NotSortable
            End With
            .Columns.Add(colCODate)

            Do Until pDateCI = pDateCO.AddDays(-1)
                pDateCI = pDateCI.AddDays(1)
                .Columns.Add((Format(pDateCI, "yyyyMMddHHmmss")), (Format(pDateCI, "yyyy/MM/dd") & " " & pTimeCI))
                .Columns.Add((Format(pDateCI.AddDays(1), "yyyyMMddHHmmss")), (Format(pDateCI.AddDays(1), "yyyy/MM/dd") & " " & pTimeCO))
            Loop

            .GridColor = Color.Blue
            .DefaultCellStyle.ForeColor = Color.Black
            .RowsDefaultCellStyle.BackColor = Color.WhiteSmoke
            .AlternatingRowsDefaultCellStyle.BackColor = Color.White
            .GridColor = Color.Silver

        End With

    End Sub





with error:
Sum of the columns' FillWeight values cannot exceed 65535.
in looping line (
.Columns.Add((Format(pDateCI.AddDays(1), "yyyyMMddHHmmss")), (Format(pDateCI.AddDays(1), "yyyy/MM/dd") & " " & pTimeCO)))

)

how can i resolve this problem, please...
thanks
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100