Hala mahala
Hala mahala

Reputation: 151

How to hide a DataGridViewButtonCell

I have a DataGridViewButtonCell in my DataGridView and I wanted to set the property Visible to True.

I have tried:

DataGridView1.Rows("number of row i want").Cells("number of cell i want").Visible = True

Unfortunately it says that the property visible is read only.

Here is the code:

Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        'does not work
        DataGridView1.Rows(e.RowIndex).Cells(6).Visible = True         
End Sub

Does anyone knows how I can achieve this?

Thanks.

Upvotes: 2

Views: 5584

Answers (2)

JohnG
JohnG

Reputation: 9469

This is really a perspective issue. From a programmer’s perspective, simply ignoring the button clicks on the buttons I want to disable is very easy to do and takes just a few lines of code.

From a user perspective, this situation would play out like this… the user clicks what appears to be a valid enabled button, and nothing happens. The user did not write the code for this… so at best the user will think the computer is not responding to the button click or at the worst… would think your coding skills are dubious!

The same situation happens if the button is missing. The user is not going to know why it is missing… but will most likely come to the same conclusion described above with a non-working button.

In another very simple approach, let say that all the buttons are enabled and we have a list of the button indexes we want to disable. The users presses one of the buttons, we check the disabled button list and if the clicked button is one that is disabled, simply display a message box to indicate why this button is disabled. This approach says to the user… “Here are a bunch of buttons, guess which ones are enabled”…

The DataGridViewDisableButtonCell and DataGridViewDisableButtonColumn wrappers solve all of the above issues… the button is visible so the user wont question where the button went if you set it to invisible and it is greyed out. “Greyed out” is something most users understand and will relieve the user of having to “guess” which buttons are enabled.

You can create a wrapper for two classes: the DataGridViewButtonCell and the DataGridViewButtonColumn.

The link How to: Disable Buttons in a Button Column in the Windows Forms DataGridView Control to the MS example is one I have used before using C#, however there is a VB implementation at the link also.

Below is a picture of the result of using the two wrappers described in the MS link. For testing, the picture below uses the check boxes to left of the button to disable the button on the right.

IMHO, using this strategy is user friendly. If you simply make the button invisible or read only, then the user is possibly going to think your code is messed up and not have a clear understanding of WHY the button is missing or doesn’t work. A disabled button indicates to the user that the button is not available for that item. An option would be to have a mouse roll-over indicating why the button is disabled.

enter image description here

Upvotes: 1

Bugs
Bugs

Reputation: 4489

There is no actual way to hide a DataGridViewButtonCell. Currently I can only see two options:

  1. Use padding to move the button over as shown here. I will provide similar VB.NET code
  2. Set the Cell to a DataGridViewTextBoxCell and set the ReadOnly property to True

Use Padding:

Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    If DataGridView1.Rows(e.RowIndex).Cells(6).GetType() Is GetType(DataGridViewButtonCell) Then
        Dim columnWidth As Integer = DataGridView1.Columns(e.ColumnIndex).Width

        Dim newDataGridViewCellStyle As New DataGridViewCellStyle With {.Padding = New Padding(columnWidth + 1, 0, 0, 0)}

        DataGridView1.Rows(e.RowIndex).Cells(6).Style = newDataGridViewCellStyle
    End If
End Sub

Use DataGridViewTextBoxCell:

Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    If DataGridView1.Rows(e.RowIndex).Cells(6).GetType() Is GetType(DataGridViewButtonCell) Then
        Dim newDataGridViewCell As New DataGridViewTextBoxCell

        DataGridView1.Rows(e.RowIndex).Cells(6) = newDataGridViewCell

        newDataGridViewCell.ReadOnly = True
    End If
End Sub

Both of these should give you the effect of not showing the button.

Upvotes: 1

Related Questions