locked
moving and resizing custom control at runtime in datagridview RRS feed

  • Question

  • i'm attempting to develop a calendar using datagridview where column headers are dates and row headers are list of employees.

    i would like to add a button/custom control on the datagridview to represent a task over time where I can move or resize at runtime.

    Is this possible? If yes, kindly point me to the right direction.

    Thanks in advance.

    Thursday, November 25, 2010 1:47 AM

Answers

All replies

  • Hello tsluu,


    Thanks for your post.


    If I understand your problem correctly, the column headers are dates and the row headers are list of employees. When the task is over time, a button will be added to the datagridview column, and its size will be changed according to the overdue time. Is it right?


    Below code maybe what you want. It is just an simple example. You need to make some changes according to your scenario. If I

    misunderstood, could you please describe your problem more specific? How do you want to judge the task is over time? How do you bound the dataset? 

     

    Public Class Form1

        Dim Employee() As String = {"Employee1", "Employee2", "Employee3", "Employee4", "Employee5"}

        Dim i As Integer = 0

        Dim j As Integer = 0

        Dim DataGridView1 As New DataGridView

        Dim columnHeaderStyle As New DataGridViewCellStyle()

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            DataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle

            DataGridView1.Width = 512

            DataGridView1.RowHeadersWidth = 100

            DataGridView1.Columns.Add("1", "2010-11-25")

            DataGridView1.Columns.Add("2", "2010-11-26")

            DataGridView1.Columns.Add("3", "2010-11-27")

            Me.Controls.Add(DataGridView1)

            For i = 0 To 4

                Dim row As DataRow

                DataGridView1.Rows.Add(row)

                DataGridView1.Rows(i).HeaderCell.Value = Employee(i).ToString()

            Next

        End Sub

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            For j = 0 To 2

                For i = 0 To 4

                    Dim index As Integer = Me.DataGridView1.Rows.Count - i - 2

                    Dim btn1 As New Button()

                    btn1.Name = "btnRun" & index.ToString()

                    btn1.Width = 30 + j * 10

                    Me.DataGridView1.Columns(j).Width = 100

                    Me.DataGridView1.Controls.Add(btn1)

                    btn1.Location = New System.Drawing.Point(((Me.DataGridView1.GetCellDisplayRectangle(j, index, True).Left)), Me.DataGridView1.GetCellDisplayRectangle(j, index, True).Y)

                Next

            Next

        End Sub

    End Class

     

    Best regards,
    Liliane
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. Thanks
    Thursday, November 25, 2010 7:16 AM
  • Hi Liliane,

     

    Thanks for your response. You hv showed me how to add buttons into the datagridview. Thats good.

    The button represents a task for an employee for a day.

    However, a task may drag on for a few days. For that situation, I would like click on the right side of the button and extend the width of the  button to span over columns, as the columns represent days. If possible, i would like to move the whole button within the same row, as row represent an employee.

    Of cos, the buttons would be bound to a dataset otherwise it wont make sense. that would be a next step for me.

    Thursday, November 25, 2010 8:33 AM
  • Hello tsluu,

    Do you mean such effects?

    If so, you could drag a button to the datagridview column, then move and size the controls at runtime.

    Please check below articles about how to move controls at runtime. Hope they could make you get some ideas.

    http://www.codeproject.com/KB/vb/RuntimeResizer.aspx
    (Creating/Moving/Sizing Controls at Runtime)
    http://www.codeproject.com/KB/dialog/MoveYourControls.aspx
    (Move controls on a form at runtime)

    If you have any concerns, please feel free to follow up.

    Best regards,
    Liliane
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. Thanks
    • Proposed as answer by Cor Ligthert Monday, November 29, 2010 7:37 AM
    • Marked as answer by tsluu Monday, November 29, 2010 9:10 AM
    Monday, November 29, 2010 6:02 AM
  • Hi Liliane,

    Thanks for your reply.

    I've tried using the methods you suggested and found that with datagridview, it failed when you try to scroll to the right. The buttons can move onto the rowheader. Unless i can make the buttons such that they go behind the rowheaders when the buttons are over it. That will look better.

    Monday, November 29, 2010 3:24 PM