none
Buttons in DataGridview

    Question

  • Hi All, Is it possible to add a small button to a DataGridView cell. or is this only possible with commercial grids? See example below.

    Regards,

    Mark

    Friday, February 17, 2017 1:08 PM

All replies

  • If you are referring to the cell next to where the arrow is pointing, yes you can add a button using the DataGridViewButtonColumn Class.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, February 17, 2017 2:18 PM
  • You ask a simple question and yet display a complex control. Is there an assumption to make a complex control as displayed, inheriting a DataGridView, using a button to expand or collapse rows?

    There is method to provide button to a particular cell in a DataGridView also which is not adding a button column to a DataGridView AFAIK.


    La vida loca

    Friday, February 17, 2017 5:45 PM
  • I suppose you could set the tag property of the button to know which column and row index a particular button is being used from.

    Option Strict On
    
    Public Class Form1
    
        WithEvents TestDataTable As New DataTable("Test")
        Dim BT As New Button
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
    
            DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect
            DataGridView1.MultiSelect = False
            DataGridView1.AllowUserToAddRows = True
            DataGridView1.AllowUserToDeleteRows = True
            DataGridView1.AllowDrop = True
            DataGridView1.AutoSize = True
            DataGridView1.ColumnHeadersDefaultCellStyle.Font = New Font("Book Antiqua", 11)
            DataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            DataGridView1.RowsDefaultCellStyle.Font = New Font("Cambria", 11)
    
            TestDataTable.Columns.Add("Column 1", GetType(String))
            TestDataTable.Columns.Add("Column 2", GetType(String))
            TestDataTable.Columns.Add("Column 3", GetType(String))
            TestDataTable.Columns.Add("Column 4", GetType(String))
    
            TestDataTable.Rows.Add("Row1 Col1", "Row1 Col2", "Row1 Col3", "Row1 Col4")
            TestDataTable.Rows.Add("Row2 Col1", "Row2 Col2", "Row2 Col3", "Row2 Col4")
            TestDataTable.Rows.Add("Row3 Col1", "Row3 Col2", "Row3 Col3", "Row3 Col4")
            TestDataTable.Rows.Add("Row4 Col1", "Row4 Col2", "Row4 Col3", "Row4 Col4")
    
            DataGridView1.DataSource = TestDataTable
            With BT
                .Text = "+"
                .Font = New Font("Book Antiqua", 9)
                .Visible = False
                .Name = "Button1"
                .TextAlign = ContentAlignment.MiddleCenter
            End With
            AddHandler BT.Click, AddressOf BT_Click
            DataGridView1.Controls.Add(BT)
        End Sub
    
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            BT.Location = DataGridView1.GetCellDisplayRectangle(1, 2, True).Location
            BT.Size = New Size(DataGridView1.GetCellDisplayRectangle(1, 2, True).Size.Height, DataGridView1.GetCellDisplayRectangle(1, 2, True).Size.Height)
            BT.Visible = True
        End Sub
    
        Private Sub BT_Click(sender As Object, e As EventArgs)
            MessageBox.Show(DirectCast(sender, Button).Name)
        End Sub
    
    End Class
    


    La vida loca

    Friday, February 17, 2017 6:13 PM
  • For a simple button, yes this is possible as already shown. If you want collapsing  functionality as shown, that would be for a custom commercial Grid e.g. http://www.telerik.com/products/winforms/gridview.aspx

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, February 17, 2017 6:39 PM
    Moderator
  • For a simple button, yes this is possible as already shown. If you want collapsing  functionality as shown, that would be for a custom commercial Grid e.g. http://www.telerik.com/products/winforms/gridview.aspx

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Well you could set rows visibility to False perhaps by selecting the Button if you knew what row the Button was in and therefore what rows to set visibility to False for and back to True for depending on the Text in the Button maybe.

    The functionality of the DataGridView may be compromised for all I know by doing this though.

    How can I set the distance from last row to the end of datagridview ?

    count datagridview rows problem

    Option Strict On
    
    Public Class Form1
    
        WithEvents TestDataTable As New DataTable("Test")
        Dim BT As New Button
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
    
            DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect
            DataGridView1.MultiSelect = False
            DataGridView1.AllowUserToAddRows = True
            DataGridView1.AllowUserToDeleteRows = True
            DataGridView1.AllowDrop = True
            DataGridView1.AutoSize = True
            DataGridView1.ColumnHeadersDefaultCellStyle.Font = New Font("Book Antiqua", 11)
            DataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            DataGridView1.RowsDefaultCellStyle.Font = New Font("Cambria", 11)
    
            TestDataTable.Columns.Add("Column 1", GetType(String))
            TestDataTable.Columns.Add("Column 2", GetType(String))
            TestDataTable.Columns.Add("Column 3", GetType(String))
            TestDataTable.Columns.Add("Column 4", GetType(String))
    
            TestDataTable.Rows.Add("Row1 Col1", "Row1 Col2", "Row1 Col3", "Row1 Col4")
            TestDataTable.Rows.Add("Row2 Col1", "Row2 Col2", "Row2 Col3", "Row2 Col4")
            TestDataTable.Rows.Add("Row3 Col1", "Row3 Col2", "Row3 Col3", "Row3 Col4")
            TestDataTable.Rows.Add("Row4 Col1", "Row4 Col2", "Row4 Col3", "Row4 Col4")
            TestDataTable.Rows.Add("Row5 Col1", "Row5 Col2", "Row5 Col3", "Row5 Col4")
            TestDataTable.Rows.Add("Row6 Col1", "Row6 Col2", "Row6 Col3", "Row6 Col4")
            TestDataTable.Rows.Add("Row7 Col1", "Row7 Col2", "Row7 Col3", "Row7 Col4")
            TestDataTable.Rows.Add("Row8 Col1", "Row8 Col2", "Row8 Col3", "Row8 Col4")
            TestDataTable.Rows.Add("Row9 Col1", "Row9 Col2", "Row9 Col3", "Row9 Col4")
            TestDataTable.Rows.Add("Row10 Col1", "Row10 Col2", "Row10 Col3", "Row10 Col4")
    
            DataGridView1.DataSource = TestDataTable
            With BT
                .Text = "+"
                .Font = New Font("Book Antiqua", 9)
                .Visible = False
                .Name = "Button1"
                .TextAlign = ContentAlignment.MiddleCenter
                .Tag = 2
            End With
            AddHandler BT.Click, AddressOf BT_Click
            DataGridView1.Controls.Add(BT)
        End Sub
    
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            BT.Location = DataGridView1.GetCellDisplayRectangle(1, 2, True).Location
            BT.Size = New Size(DataGridView1.GetCellDisplayRectangle(1, 2, True).Size.Height, DataGridView1.GetCellDisplayRectangle(1, 2, True).Size.Height)
            BT.Visible = True
        End Sub
    
    
        Private Sub BT_Click(sender As Object, e As EventArgs)
            Select Case DirectCast(sender, Button).Text
                Case Is = "+"
                    DirectCast(sender, Button).Text = "-"
                    For i = CInt(DirectCast(sender, Button).Tag) + 1 To CInt(DirectCast(sender, Button).Tag) + 4
                        DataGridView1.Rows(i).Visible = False
                    Next
                Case Is = "-"
                    DirectCast(sender, Button).Text = "+"
                    For i = CInt(DirectCast(sender, Button).Tag) + 1 To CInt(DirectCast(sender, Button).Tag) + 4
                        DataGridView1.Rows(i).Visible = True
                    Next
            End Select
        End Sub
    
    End Class
    


    La vida loca

    Friday, February 17, 2017 7:07 PM
  • For a simple button, yes this is possible as already shown. If you want collapsing  functionality as shown, that would be for a custom commercial Grid e.g. http://www.telerik.com/products/winforms/gridview.aspx


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Well you could set rows visibility to False perhaps by selecting the Button if you knew what row the Button was in and therefore what rows to set visibility to False for and back to True for depending on the Text in the Button maybe.

    The functionality of the DataGridView may be compromised for all I know by doing this though.

    How can I set the distance from last row to the end of datagridview ?

    count datagridview rows problem


    La vida loca

    I would not go that direction, in short it would simply cause one to write more code along with if the person implementing this needs to do formatting via CellFormatting event of the control that makes things complicated.

    If I were still writing desktop apps in vb.net I had no allowance for purchasing a control I would look at the following done in C# but once it's compiled language does not matter..

    https://www.codeproject.com/tips/995958/datagridviewgrouper


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, February 17, 2017 7:28 PM
    Moderator
  • For a simple button, yes this is possible as already shown. If you want collapsing  functionality as shown, that would be for a custom commercial Grid e.g. http://www.telerik.com/products/winforms/gridview.aspx


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Well you could set rows visibility to False perhaps by selecting the Button if you knew what row the Button was in and therefore what rows to set visibility to False for and back to True for depending on the Text in the Button maybe.

    The functionality of the DataGridView may be compromised for all I know by doing this though.

    How can I set the distance from last row to the end of datagridview ?

    count datagridview rows problem


    La vida loca

    I would not go that direction, in short it would simply cause one to write more code along with if the person implementing this needs to do formatting via CellFormatting event of the control that makes things complicated.

    If I were still writing desktop apps in vb.net I had no allowance for purchasing a control I would look at the following done in C# but once it's compiled language does not matter..

    https://www.codeproject.com/tips/995958/datagridviewgrouper


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    I wouldn't either but it's possible to perform.

    La vida loca

    Friday, February 17, 2017 7:38 PM
  • Hi Karen,

    I looked at the codeproject solution. Is it possibe if I'm writing in VB to combine this with C#?

    Regards,

    Mark

    Sunday, February 19, 2017 12:50 PM
  • Very nice one! I'm bussy with it. Still to complicated for me to write such code.

    Regards,

    Mark

    Sunday, February 19, 2017 12:51 PM
  • If you're considering third-party data grids, give this one a look:

    https://www.devexpress.com/Products/NET/Controls/WinForms/Grid/

    It's not cheap but it performs quite well and is very flexible.


    "One who has no vices also has no virtues..."

    Sunday, February 19, 2017 1:52 PM
  • Hi Karen,

    I looked at the codeproject solution. Is it possibe if I'm writing in VB to combine this with C#?

    Regards,

    Mark

    Telerik has online C# to VB converter as well as JustDecompile to decompile an assembly which can then be decompiled to C# or VB.Net or as long as the assembly was written in C# or VB.Net. JustDecompile can also output to a Visual Studio project of the conversion.

    If you learn how to use those, although a conversion may need to be corrected since all C# can not be converted to VB.Net and vice versa, then you will be able to use things written in C# for VB.Net.


    La vida loca

    Sunday, February 19, 2017 6:24 PM
  • Hi,

    I implemented your code in mine. One question. If I scroll in my DGV, the button stays on the same position, and the cells are scrolling behind the button. Is it possible to let the button control scroll with the cell where it is displayed?

    Regards,

    Mark

    Monday, February 20, 2017 10:38 AM
  • Hi,

    I implemented your code in mine. One question. If I scroll in my DGV, the button stays on the same position, and the cells are scrolling behind the button. Is it possible to let the button control scroll with the cell where it is displayed?

    Regards,

    Mark


    Hi Mark Hofland,

    According to your description, I am not sure what's your issue. Could you please describe more about your problem/issue? Such as post the some screenshots. It will help us analyze and diagnose this issue more clearly.

    In addition, if your posting problem has been solved, please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Best Regards,

    Neda Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 22, 2017 6:13 AM
    Moderator