Question

I have a GridView with edit and delete enabled. I want the edit and delete to be disabled or hidden if a specific column for that row in the GridView has a value:

enter image description here

In this case I want the default edit/delete options of the GridView do be unavailable for that has a value in the Name column. This means only the 3rd column will be editable.

I am making use of the default GridView controls:

<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />

If Edit is clicked the <EditItemTemplate> for the row is displayed and the Ward and Sector values can be changed. The GridView is attached to an ObjectDataSource that is linked to a class that has the CRUD operations.

Was it helpful?

Solution

You can parse out the value of the ID field and set the visibility of the edit/delete buttons in the gridview's RowDataBound event. Here's how I do it, the following code goes in your gridview's RowDataBound event handler:

    If e.Row.RowType = DataControlRowType.DataRow Then
        'Get reference to the button you want to hide/show
        Dim delButton As ImageButton = CType(e.Row.Cells(3).Controls(2), ImageButton)
        'check your data for value
        Dim bIsThereAValue as Boolean = DoACheckForAValue(YourGridView.DataKeys(e.Row.RowIndex).Value)
        delButton.Visible = bIsThereAValue
    End If

This method works well for me, I often use this where this item may be used in another entity, I disable being able to delete the item (for example this grid shows available pictures, but the picture is being used in a gallery, then I disable being able to delete the picture until it is no longer part of a gallery).

You'll want to make sure you do an adequate check for your column's value in, I'm unsure offhand, if IsNothing would work, or if you would have to compare to string.empty or something, but it shouldn't be too difficult.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top