views:

244

answers:

2

Hi folks,

I have a datagridview on a form with some data. The 1st column contains button for deleting the row. How can we disable this button or the entire row based on some condition, so the row cannot be deleted?

TIA

+1  A: 

There's actually a HowTo on MSDN doing exactly this.

Edit: Added some other suggestions.

You can make the button column invisible.

Or if you only want to disable deletion of certain rows you could put in true or false in each DataGridViewRows Tag property and in your button event handler you only delete the ones that are set to false. You could possibly combine this with just changing the foreground and background colours of the cell to make it look disabled, this colouring in could probably be done in the CellFormatting event handler or something so that you don't have to loop through and colour it in by hand.

ho1
I have seen that before but there's got to be a better way.
SoftwareGeek
@BhejaFry: Well, a lot of that code is to make it look right. If you don't care about that there are lots of other ways, I'll add a couple of suggestions to my answer since it's easier to read there.
ho1
A: 

Would you consider just turning the button cell into a regular empty text box disabled?

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

It loses its bordered "Button" appearance and blends in with the remainder of the cells (assuming you are using primarily the default DataGridViewTextBoxCells).

Shiftbit