Search code examples
winformsbuttondatagridviewdelete-row

Datagridview, disable button/row


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?


Solution

  • 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.