XtraGrid 在特定单元格的鼠标移动时禁用存储库项目按钮

XtraGrid Disable repository item button on specific cell's mouse move

我想在其他特定单元格中输入鼠标时禁用 ButtonEdit 编辑器单元格。我怎样才能实现这个功能?

就像,每当我的鼠标悬停在 Ammar 单元格上时,必须禁用 Remove 按钮,就像在第一行和第三行中一样。

注意: Select 列是 RepositoryItemButtonEdit 列。

这是我的 MouseMove 活动:

Private Sub gridViewLevel1_MouseMove(sender As Object, e As MouseEventArgs)
    ' Get a View at the current point.
    Dim view As BaseView = GridControl.GetViewAt(e.Location)
    ' Retrieve information on the current View element.
    Dim baseHI As BaseHitInfo = view.CalcHitInfo(e.Location)
    Dim gridHI As GridHitInfo = TryCast(baseHI, GridHitInfo)
    'Get Field Value
    Dim fieldName As String = gridHI.View.GetRowCellValue(gridHI.RowHandle, gridHI.Column)

    Dim row As DataRow = Nothing

    If Not gridHI Is Nothing Then
        lblHitInfo.Text = fieldName
        row = gridHI.View.GetDataRow(gridHI.RowHandle)
    Else

    End If

End Sub

就这样,我得到了DataRow但是接下来要做什么呢?

我不是很清楚你想达到什么目的,但我会尝试使用 CustomRowCellEdit 事件来根据行数据影响编辑器。在这种情况下,您可以根据您的逻辑设置 e.RepositoryItem,例如将其设置为 buttonedit 的只读实例或非只读实例。

通常,您可以像这样将行对象转换为您的实际数据类型:

var myData = gridView.GetRow(rowHandle) as MyData;

if(myData.Foo)
   e.RepositoryItem = myReadonlyEditor;
else
   e.RepositoryItem = myNormalEditor;

得到 问题的答案后!我能够解决这个问题。

本次活动我修改如下!

Private Sub GrdView_MouseMove(sender As Object, e As MouseEventArgs)
    ' Get a View at the current point.
    Dim view As BaseView = GrdCntrlMain.GetViewAt(e.Location)
    ' Retrieve information on the current View element.
    Dim baseHI As BaseHitInfo = view.CalcHitInfo(e.Location)
    Dim gridHI As GridHitInfo = TryCast(baseHI, GridHitInfo)

    If gridHI.InColumn Or gridHI.InDataRow Then
        Dim cellValue As String = gridHI.View.GetRowCellValue(gridHI.RowHandle, gridHI.Column)
        If cellValue = "Ammar" Then
            GrdView.FocusedRowHandle = gridHI.RowHandle
            GrdView.FocusedColumn = GrdView.Columns("Select")

            GrdView.ShowEditor()

            Dim editor = GrdView.ActiveEditor

            editor.Enabled = False

        Else

            GrdView.FocusedRowHandle = gridHI.RowHandle
            GrdView.FocusedColumn = GrdView.Columns("Select")

            GrdView.ShowEditor()

            Dim editor = GrdView.ActiveEditor

            editor.Enabled = True
        End If
    End If
End Sub

虽然它会在每个单元格上调用,但您可以将它限制为仅针对一列。

您可以使用GridView.CustomDrawCell event to draw the disabled state in cell. For this you need to call GridView.InvalidateRowCell方法在每次单元格满​​足您的条件时重新绘制特定单元格。
这是示例:

Private disabledRowHandle? As Int32
Private disabledColumn As GridColumn

Private Sub GrdView_MouseMove(sender As Object, e As MouseEventArgs) Handles GrdView.MouseMove
    ' Get a View at the current point.
    Dim view = TryCast(GrdCntrlMain.GetViewAt(e.Location), GridView)
    ' Retrieve information on the current View element.
    Dim gridHI = view.CalcHitInfo(e.Location)

    Dim redrawPrevious = True
    Dim drawDisabled = False

    If gridHI.InColumn Or gridHI.InDataRow Then
        Dim cellValue As String = gridHI.View.GetRowCellValue(gridHI.RowHandle, gridHI.Column)

        If cellValue = "Ammar" Then
            If Not disabledRowHandle.Equals(gridHI.RowHandle) Then
                drawDisabled = True
            Else
                redrawPrevious = False
            End If
        End If
    End If

    If redrawPrevious AndAlso disabledRowHandle.HasValue Then
        Dim rowHandle = disabledRowHandle.Value
        disabledRowHandle = Nothing

        view.InvalidateRowCell(rowHandle, disabledColumn)
    End If

    If drawDisabled Then
        If IsNothing(disabledColumn) Then
            disabledColumn = GrdView.Columns("Select")
        End If

        disabledRowHandle = gridHI.RowHandle
        view.InvalidateRowCell(disabledRowHandle.Value, disabledColumn)
    End If
End Sub

Private Sub GrdView_CustomDrawCell(sender As Object, e As RowCellCustomDrawEventArgs) Handles GrdView.CustomDrawCell
    If disabledRowHandle.Equals(e.RowHandle) AndAlso e.Column Is disabledColumn Then
        Dim currentColor = e.Appearance.ForeColor
        Dim currentUseForeColor = e.Appearance.Options.UseForeColor

        e.Appearance.ForeColor = CommonSkins.GetSkin(UserLookAndFeel.Default).Colors.GetColor(CommonColors.DisabledText)
        e.Appearance.Options.UseForeColor = True

        e.DefaultDraw()

        e.Appearance.ForeColor = currentColor
        e.Appearance.Options.UseForeColor = currentUseForeColor

        e.Handled = True
    End If
End Sub