GridView 如果列有值就禁止编辑/删除[英] GridView Disable edit/delete if column has value

本文是小编为大家收集整理的关于GridView 如果列有值就禁止编辑/删除的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

我有一个带有编辑并启用删除的GridView.我希望如果该行的特定列中的GridView具有值:

在此处输入图像说明

在这种情况下,我希望GridView的默认编辑/删除选项确实不可用,因为它在名称列中具有值.这意味着只有第三列才能编辑.

我正在使用默认的GridView控件:

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

如果单击编辑,则将显示该行的<EditItemTemplate>,并且可以更改病房和部门值. GridView连接到链接到具有Crud操作的类的对象DataSource.

推荐答案

您可以解析ID字段的值,并在GridView的RowDatabound事件中设置编辑/删除按钮的可见性.这是我的操作方式,以下代码在您的GridView的RowDatabound事件处理程序中进行:

    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

此方法对我来说很好,我经常在另一个实体中使用此项目,我禁用能够删除该项目(例如,此网格显示可用的图片,但图片已在画廊中使用,然后我禁用能够删除图片,直到不再是画廊的一部分).

您需要确保您对专栏的价值进行足够的检查,我不确定,如果iSning不起作用,或者如果您必须将其比较到string.empt..empt.太难了.

本文地址:https://www.itbaoku.cn/post/268619.html

问题描述

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.

推荐答案

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.