Datagridview, disable button / row

I have a datagridview on a form with some data. The first column contains a button to delete a row. How can we turn off this button or the entire line based on some condition, so the line cannot be deleted?

+6
button winforms delete-row datagridview
source share
3 answers

In fact, HowTo on MSDN does just that.

Edit: other suggestions added.

You can make the button column invisible.

Or, if you only want to disable the deletion of certain rows, you can put true or false in each property of the DataGridViewRow Tag , and in the button event handler you delete only those that are set to false. You could combine this by simply changing the foreground and background colors in the cell so that it looks off, this coloring could probably be done in the CellFormatting event CellFormatting or something that you don’t need to scroll and the color is manual.

+3
source share

Could you just turn off the button cell in the plain empty text box?

 Dim cell As DataGridViewButtonCell = dgv.row(x).cell(y) cell = New DataGridViewTextBoxCell() cell.value = String.Empty cell.ReadOnly = True 

It loses its "Button" border appearance and mixes with the rest of the cells (assuming you use the default DataGridViewTextBoxCells).

Here's the equivalent in C #, plus it highlights the field so that it looks read-only:

 var cell = dgv[column, row] = new DataGridViewTextBoxCell(); cell.Value = ""; // ignored if this column is databound cell.ReadOnly = true; cell.Style.BackColor = Color.FromKnownColor(KnownColor.Control); 
+8
source share

This is an old post, but I would suggest what I do.

 If conditionToDisable Then Dim cell As New DataGridViewTextBoxCell 'Replace the ButtonCell for a TextCell' cell.Value = valueForCell 'Set the value again' grid.Rows(r).Cells(c) = cell 'Override the cell' End If 

I hope this will be helpful.

+3
source share

All Articles