Description:
I want to display disabled buttons in a ButtonEdit column for a particular cell depending upon some condition. How can I do this?
Answer:
There are three ways to implement this task:
- Create two ButtonEdit repository items. One with the enabled button and another with the disabled button. Then handle the GridView.CustomRowCellEdit event and pass the necessary repository item to the e.RepositoryItem parameter according to a specific condition. Please see the Assigning Editors to Individual Cells help topic for additional information.
2. Create a custom button editor. (Custom editor creation is described in the Custom Editors article.) Then, implement a custom ViewInfo class and override the OnBeginPaint() method as shown below.
C#public class CustomButtonEditViewInfo : ButtonEditViewInfo {
protected override void OnBeginPaint() {
base.OnBeginPaint();
ApplyState();
}
public void ApplyState() {
ButtonStateEventArgs e = new ButtonStateEventArgs(Tag);
(this.Item as RepositoryItemCustomButtonEdit).RaiseButtonStateEvent(e);
if (!e.IsEnable && this.RightButtons.Count > 0) {
this.RightButtons[0].State = DevExpress.Utils.Drawing.ObjectState.Disabled;
}
}
public CustomButtonEditViewInfo(RepositoryItem item)
: base(item) {
}
}
Visual BasicPublic Class CustomButtonEditViewInfo
Inherits ButtonEditViewInfo
Protected Overrides Sub OnBeginPaint()
MyBase.OnBeginPaint()
ApplyState()
End Sub
Public Sub ApplyState()
Dim e As New ButtonStateEventArgs(Tag)
TryCast(Me.Item, RepositoryItemCustomButtonEdit).RaiseButtonStateEvent(e)
If (Not e.IsEnable) AndAlso Me.RightButtons.Count > 0 Then
Me.RightButtons(0).State = DevExpress.Utils.Drawing.ObjectState.Disabled
End If
End Sub
Public Sub New(ByVal item As RepositoryItem)
MyBase.New(item)
End Sub
End Class
Additionally, implement the GetButtonState event in a custom RepositoryItem class. Handle it to pass any custom condition to the editor.
Please see the How to display disabled buttons for particular cells within a ButtonEdit column example (or attached example) to get a detailed solution.
See Also:
How to Conditionally Prevent Editing for Individual Grid Cells
How to customize the Look-And-Feel of my grid cells
How to make my grid columns read-only
How to change the editor's button text depending upon the grid row
How do I get the value from this cell or specially other cells in the gridview on the ShownEditor event?
The "e" argument doesn't have a row handle.
@Cesar - It seems that this question is not connected to the topic. Please create a new ticket in our Support Center. We will answer it as soon as possible.
Hello,
Is that solution still the way to go to achieve this?
It worked ok, the only problem I am having is that when the mouse is over the button, since the state is set to normal, then the border is not displayed as you would expect it since it has the focus.
I attached a screenshot to show what I mean.
Thanks
I suggest that you use the first approach recommended in this article:
>>
Create two ButtonEdit repository items. One with the enabled button and another with the disabled button. Then handle the GridView.CustomRowCellEdit event and pass the necessary repository item to the e.RepositoryItem parameter according to a specific condition.
<<
In this case buttons should be displayed correctly.
Let us know if you encounter additional questions regarding this issue. We will be glad to help you.
In my case I have a repository with 2 buttons and each button can enabled/disabled independently so if I want to cover all the cases I need 4 repositories which is not ideal.
Anything else I could do?
Thanks.
Hi Carole,
To process your inquiry in the most efficient manner, I've created a separate ticket on your behalf:
How to display ButtonEdit with disabled buttons for particular cells
Please refer to it for further correspondence.
This link at the end of the article seems to be to an internal system?
http://isc.devexpress.com/Thread/WorkplaceDetails/T100881
I am interested to see the example regarding creating a GetButtonState event, I already have a inherited ButtonEdit and ViewInfo as I have implemented the ability to not have the buttons stretch the entire width of the editor using another example.
What I am trying to work out is that during the GetButtonState event of the RepositoryItem how would you get the current row information.
Hello Michael,
Thank you for informing us about this issue. I've updated the link in the article so now you can refer to this example.