In WinForms, how can I create a delete button in a DevExpress GridControl?

11,462

Solution 1

I discovered that there is actually a delete button kind. So, I do everything as in the question, but instead of choosing the kind Glyph, I choose Delete, and I don't need to select an image.

Solution 2

I have summarized what I found in the DevExpress forum:

Use the ButtonEdit control and set the TextEditStyle property to HideTextEditor. The Repository Item has a Buttons collection through which you can add a caption, image etc.

In the Buttons collection, change the "Kind" property to "Glyph". You can use the CustomRowCellEdit event to conditionally apply editors on a cell-by-cell basis. Make sure you set the Button's Kind property to "Glyph" and set the Caption property to whatever text you'd like:

DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit buttonEdit = 
    new DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit();
buttonEdit.Buttons[0].Kind = DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph;
buttonEdit.Buttons[0].Caption = "X";
buttonEdit.TextEditStyle =             
    DevExpress.XtraEditors.Controls.TextEditStyles.HideTextEditor;
e.RepositoryItem = buttonEdit;

You should handle the GridView's CustomRowCellEdit event, construct a new RepositoryItemButtonEdit and assign it to the e.RepositoryItem property.

Let me know if that works.

Share:
11,462
Stephen Oberauer
Author by

Stephen Oberauer

Senior .NET developer / former tech lead (C#, SQL, Angular, Vue.js, WPF, JavaScript, HTML, etc., etc., etc...) Other interests: Writing (The Mischievous Nerd's Guide to World Domination, Transpolitica, blog), Video Making (Especially 3D - stereoscopic), Skateboarding, AI, VR, Music (Mainly electric guitar) Very interested in the future, especially the future of technologies such as AI and VR. Also very interested in initiatives to make the world run more intelligently. Wouldn't it be nice if the world ran on a system that worked as well as stackoverflow.com?

Updated on June 05, 2022

Comments

  • Stephen Oberauer
    Stephen Oberauer almost 2 years

    I'm trying to create a delete button at the right of every row in a DevExpress GridControl, like this:

    enter image description here

    What I've done is added another column and set its ColumnEdit property to an instance of RepositoryItemButtonEdit. I handle the ButtonClick event, to delete a row.

    I can determine which row I'm on from this code:

    myGridView.GetRow(myGridView.FocusedRowHandle);
    

    Because I don't want a text editor on my button, I set the TextEditStyle to HideTextEditor.

    By default, the button shows an ellipsis.

    To remove the ellipsis, I adjusted the Buttons property on the RepositoryItemButtonEdit. I set the Kind to Glyph and set the image to my X icon.

    Unfortunately that seems to simply remove the button altogether.

    Does anyone know a better way to do this, or a way to show a button with an image on it, in each grid row?