none
How can i disable my button column in datagridview RRS feed

  • Question

  • hello!

    how can i disable button in my datagridview if this code executed:

    Private Sub BunifuCustomDataGrid1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles BunifuCustomDataGrid1.CellFormatting
            For i As Integer = 0 To BunifuCustomDataGrid1.Rows.Count - 1
                If BunifuCustomDataGrid1.Rows(i).Cells(8).Value = "Not Available" Then
                    BunifuCustomDataGrid1.Rows(i).DefaultCellStyle.BackColor = Color.Red
                    BunifuCustomDataGrid1.Rows(i).DefaultCellStyle.ForeColor = Color.Black
    
                End If
            Next
        

    next to cell(8) is the column for button that i want to disable. Can anyone help me? 

    Thank you!

    Tuesday, August 28, 2018 10:20 AM

Answers

  • hello !? 

    i came to the site you guys given but i cant find the vb.net version of the code can anyone please tell me where or how can i find the vb.net code?

    thank you!

    Hi,

    Imports System.Windows.Forms.VisualStyles
    
    Public Class Form1
        Private dataGridView1 As DataGridView = New DataGridView()
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim column0 As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
            Dim column1 As DataGridViewDisableButtonColumn = New DataGridViewDisableButtonColumn()
            column0.Name = "CheckBoxes"
            column1.Name = "Buttons"
            dataGridView1.Columns.Add(column0)
            dataGridView1.Columns.Add(column1)
            dataGridView1.RowCount = 8
            dataGridView1.AutoSize = True
            dataGridView1.AllowUserToAddRows = False
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    
            For i As Integer = 0 To dataGridView1.RowCount - 1
                dataGridView1.Rows(i).Cells("Buttons").Value = "Button " & i.ToString()
            Next
            AddHandler dataGridView1.CellValueChanged, AddressOf dataGridView1_CellValueChanged
            AddHandler dataGridView1.CellClick, AddressOf dataGridView1_CellClick
            AddHandler dataGridView1.CurrentCellDirtyStateChanged, AddressOf dataGridView1_CurrentCellDirtyStateChanged
    
            Me.Controls.Add(dataGridView1)
        End Sub
        Private Sub dataGridView1_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs)
            If dataGridView1.IsCurrentCellDirty Then
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
            End If
        End Sub
    
        Public Sub dataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
            If dataGridView1.Columns(e.ColumnIndex).Name = "CheckBoxes" Then
                Dim buttonCell As DataGridViewDisableButtonCell = CType(dataGridView1.Rows(e.RowIndex).Cells("Buttons"), DataGridViewDisableButtonCell)
                Dim checkCell As DataGridViewCheckBoxCell = CType(dataGridView1.Rows(e.RowIndex).Cells("CheckBoxes"), DataGridViewCheckBoxCell)
                buttonCell.Enabled = Not CBool(checkCell.Value)
                dataGridView1.Invalidate()
            End If
        End Sub
    
        Private Sub dataGridView1_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
            If dataGridView1.Columns(e.ColumnIndex).Name = "Buttons" Then
                Dim buttonCell As DataGridViewDisableButtonCell = CType(dataGridView1.Rows(e.RowIndex).Cells("Buttons"), DataGridViewDisableButtonCell)
    
                If buttonCell.Enabled Then
                    MessageBox.Show(dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString() & " is enabled")
                End If
            End If
        End Sub
        Public Class DataGridViewDisableButtonColumn
            Inherits DataGridViewButtonColumn
    
            Public Sub New()
                Me.CellTemplate = New DataGridViewDisableButtonCell()
            End Sub
        End Class
    
        Public Class DataGridViewDisableButtonCell
            Inherits DataGridViewButtonCell
    
            Private enabledValue As Boolean
    
            Public Property Enabled As Boolean
                Get
                    Return enabledValue
                End Get
                Set(ByVal value As Boolean)
                    enabledValue = value
                End Set
            End Property
    
            Public Overrides Function Clone() As Object
                Dim cell As DataGridViewDisableButtonCell = CType(MyBase.Clone(), DataGridViewDisableButtonCell)
                cell.Enabled = Me.Enabled
                Return cell
            End Function
    
            Public Sub New()
                Me.enabledValue = True
            End Sub
    
            Protected Overrides Sub Paint(ByVal graphics As Graphics, ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, ByVal rowIndex As Integer, ByVal elementState As DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As DataGridViewCellStyle, ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, ByVal paintParts As DataGridViewPaintParts)
                If Not Me.enabledValue Then
    
                    If (paintParts And DataGridViewPaintParts.Background) = DataGridViewPaintParts.Background Then
                        Dim cellBackground As SolidBrush = New SolidBrush(cellStyle.BackColor)
                        graphics.FillRectangle(cellBackground, cellBounds)
                        cellBackground.Dispose()
                    End If
    
                    If (paintParts And DataGridViewPaintParts.Border) = DataGridViewPaintParts.Border Then
                        PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle)
                    End If
    
                    Dim buttonArea As Rectangle = cellBounds
                    Dim buttonAdjustment As Rectangle = Me.BorderWidths(advancedBorderStyle)
                    buttonArea.X += buttonAdjustment.X
                    buttonArea.Y += buttonAdjustment.Y
                    buttonArea.Height -= buttonAdjustment.Height
                    buttonArea.Width -= buttonAdjustment.Width
                    ButtonRenderer.DrawButton(graphics, buttonArea, PushButtonState.Disabled)
    
                    If TypeOf Me.FormattedValue Is String Then
                        TextRenderer.DrawText(graphics, CStr(Me.FormattedValue), Me.DataGridView.Font, buttonArea, SystemColors.GrayText)
                    End If
                Else
                    MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
                End If
            End Sub
        End Class
    End Class
    

    effect:

    Best Regards,

    Alex


    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.

    • Marked as answer by euchan09 Friday, September 14, 2018 7:20 AM
    Monday, September 10, 2018 7:36 AM

All replies

  • If I'm not mistaken, you may be using a third party DataGridView and if so start with them. Otherwise an alternate could be to set the caption to "Not Available" and in the click event check for this condition and handle it.

    Your other option is to use a custom column as shown here which is in C# but I have a VB.NET version also.


    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

    Tuesday, August 28, 2018 10:52 AM
    Moderator
  • Doesn't make sense. The button handle could just do nothing if cells(8).value="Not Available"

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Tuesday, August 28, 2018 5:13 PM
  • Hi,

    The DataGridView control includes the DataGridViewButtonCell class, which is used to display cells with user interfaces (UIs) that resemble buttons. However, the DataGridViewButtonCell does not provide a way to disable the appearance of the buttons displayed by the cell.
    DataGridViewDisableButtonCell, which is derived from DataGridViewButtonCell. This cell type provides a single Enabled property, and the code sample that can be downloaded from the property demonstrates how to customize the DataGridViewButtonCell class to display buttons that can be displayed as disabled. Set to false to draw disabled buttons in the cell. This example also defines a new column type, DataGridViewDisableButtonColumn, which displays the DataGridViewDisableButtonCell object. To demonstrate this new cell type and column type, the current value of each DataGridViewCheckBoxCell in the parent DataGridView determines whether the Enabled property of the DataGridViewDisableButtonCell in the same row is true or false.

    https://docs.microsoft.com/en-gb/dotnet/framework/winforms/controls/disable-buttons-in-a-button-column-in-the-datagrid

    Best Regards,

    Alex


    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, August 29, 2018 7:37 AM
  • Hi,

    The DataGridView control includes the DataGridViewButtonCell class, which is used to display cells with user interfaces (UIs) that resemble buttons. However, the DataGridViewButtonCell does not provide a way to disable the appearance of the buttons displayed by the cell.
    DataGridViewDisableButtonCell, which is derived from DataGridViewButtonCell. This cell type provides a single Enabled property, and the code sample that can be downloaded from the property demonstrates how to customize the DataGridViewButtonCell class to display buttons that can be displayed as disabled. Set to false to draw disabled buttons in the cell. This example also defines a new column type, DataGridViewDisableButtonColumn, which displays the DataGridViewDisableButtonCell object. To demonstrate this new cell type and column type, the current value of each DataGridViewCheckBoxCell in the parent DataGridView determines whether the Enabled property of the DataGridViewDisableButtonCell in the same row is true or false.

    https://docs.microsoft.com/en-gb/dotnet/framework/winforms/controls/disable-buttons-in-a-button-column-in-the-datagrid

    Best Regards,

    Alex


    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.

    For the record, the link you supplied is the same one I provided in C# and also provided it in VB.NET.

    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

    Wednesday, August 29, 2018 12:30 PM
    Moderator
  • Have you tried utilizing css? Something like 

    If BunifuCustomDataGrid1.Rows(i).Cells(8).Value = "Not Available" Then
                    BunifuCustomDataGrid1.Rows(i).CssClass = "DisabledLink"

    Then in your css you can do something like this

    .DisabledLink {
        pointer-events: none;
        cursor: default;
        color: black !important;
        background: red !important;
    }

    Wednesday, August 29, 2018 8:00 PM
  • hello !? 

    i came to the site you guys given but i cant find the vb.net version of the code can anyone please tell me where or how can i find the vb.net code?

    thank you!

    Saturday, September 8, 2018 6:20 AM
  • hello !? 

    i came to the site you guys given but i cant find the vb.net version of the code can anyone please tell me where or how can i find the vb.net code?

    thank you!

    Hi,

    Imports System.Windows.Forms.VisualStyles
    
    Public Class Form1
        Private dataGridView1 As DataGridView = New DataGridView()
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim column0 As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
            Dim column1 As DataGridViewDisableButtonColumn = New DataGridViewDisableButtonColumn()
            column0.Name = "CheckBoxes"
            column1.Name = "Buttons"
            dataGridView1.Columns.Add(column0)
            dataGridView1.Columns.Add(column1)
            dataGridView1.RowCount = 8
            dataGridView1.AutoSize = True
            dataGridView1.AllowUserToAddRows = False
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    
            For i As Integer = 0 To dataGridView1.RowCount - 1
                dataGridView1.Rows(i).Cells("Buttons").Value = "Button " & i.ToString()
            Next
            AddHandler dataGridView1.CellValueChanged, AddressOf dataGridView1_CellValueChanged
            AddHandler dataGridView1.CellClick, AddressOf dataGridView1_CellClick
            AddHandler dataGridView1.CurrentCellDirtyStateChanged, AddressOf dataGridView1_CurrentCellDirtyStateChanged
    
            Me.Controls.Add(dataGridView1)
        End Sub
        Private Sub dataGridView1_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs)
            If dataGridView1.IsCurrentCellDirty Then
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
            End If
        End Sub
    
        Public Sub dataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
            If dataGridView1.Columns(e.ColumnIndex).Name = "CheckBoxes" Then
                Dim buttonCell As DataGridViewDisableButtonCell = CType(dataGridView1.Rows(e.RowIndex).Cells("Buttons"), DataGridViewDisableButtonCell)
                Dim checkCell As DataGridViewCheckBoxCell = CType(dataGridView1.Rows(e.RowIndex).Cells("CheckBoxes"), DataGridViewCheckBoxCell)
                buttonCell.Enabled = Not CBool(checkCell.Value)
                dataGridView1.Invalidate()
            End If
        End Sub
    
        Private Sub dataGridView1_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
            If dataGridView1.Columns(e.ColumnIndex).Name = "Buttons" Then
                Dim buttonCell As DataGridViewDisableButtonCell = CType(dataGridView1.Rows(e.RowIndex).Cells("Buttons"), DataGridViewDisableButtonCell)
    
                If buttonCell.Enabled Then
                    MessageBox.Show(dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString() & " is enabled")
                End If
            End If
        End Sub
        Public Class DataGridViewDisableButtonColumn
            Inherits DataGridViewButtonColumn
    
            Public Sub New()
                Me.CellTemplate = New DataGridViewDisableButtonCell()
            End Sub
        End Class
    
        Public Class DataGridViewDisableButtonCell
            Inherits DataGridViewButtonCell
    
            Private enabledValue As Boolean
    
            Public Property Enabled As Boolean
                Get
                    Return enabledValue
                End Get
                Set(ByVal value As Boolean)
                    enabledValue = value
                End Set
            End Property
    
            Public Overrides Function Clone() As Object
                Dim cell As DataGridViewDisableButtonCell = CType(MyBase.Clone(), DataGridViewDisableButtonCell)
                cell.Enabled = Me.Enabled
                Return cell
            End Function
    
            Public Sub New()
                Me.enabledValue = True
            End Sub
    
            Protected Overrides Sub Paint(ByVal graphics As Graphics, ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, ByVal rowIndex As Integer, ByVal elementState As DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As DataGridViewCellStyle, ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, ByVal paintParts As DataGridViewPaintParts)
                If Not Me.enabledValue Then
    
                    If (paintParts And DataGridViewPaintParts.Background) = DataGridViewPaintParts.Background Then
                        Dim cellBackground As SolidBrush = New SolidBrush(cellStyle.BackColor)
                        graphics.FillRectangle(cellBackground, cellBounds)
                        cellBackground.Dispose()
                    End If
    
                    If (paintParts And DataGridViewPaintParts.Border) = DataGridViewPaintParts.Border Then
                        PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle)
                    End If
    
                    Dim buttonArea As Rectangle = cellBounds
                    Dim buttonAdjustment As Rectangle = Me.BorderWidths(advancedBorderStyle)
                    buttonArea.X += buttonAdjustment.X
                    buttonArea.Y += buttonAdjustment.Y
                    buttonArea.Height -= buttonAdjustment.Height
                    buttonArea.Width -= buttonAdjustment.Width
                    ButtonRenderer.DrawButton(graphics, buttonArea, PushButtonState.Disabled)
    
                    If TypeOf Me.FormattedValue Is String Then
                        TextRenderer.DrawText(graphics, CStr(Me.FormattedValue), Me.DataGridView.Font, buttonArea, SystemColors.GrayText)
                    End If
                Else
                    MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
                End If
            End Sub
        End Class
    End Class
    

    effect:

    Best Regards,

    Alex


    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.

    • Marked as answer by euchan09 Friday, September 14, 2018 7:20 AM
    Monday, September 10, 2018 7:36 AM