【问题标题】:In WinForms, how can I create a delete button in a DevExpress GridControl?在 WinForms 中,如何在 DevExpress GridControl 中创建删除按钮?
【发布时间】:2012-11-28 22:06:20
【问题描述】:

我正在尝试在 DevExpress GridControl 中每一行的右侧创建一个删除按钮,如下所示:

我所做的是添加另一列并将其 ColumnEdit 属性设置为 RepositoryItemButtonEdit 的一个实例。我处理ButtonClick事件,删除一行。

我可以通过这段代码确定我在哪一行:

myGridView.GetRow(myGridView.FocusedRowHandle);

因为我不想在我的按钮上使用文本编辑器,所以我将 TextEditStyle 设置为 HideTextEditor。

默认情况下,按钮显示省略号。

为了删除省略号,我调整了 RepositoryItemButtonEdit 上的 Buttons 属性。我将 Kind 设置为 Glyph,并将图像设置为我的 X 图标。

不幸的是,这似乎只是完全删除了按钮。

有没有人知道更好的方法来做到这一点,或者在每个网格行中显示一个带有图像的按钮?

【问题讨论】:

  • 对于这样一个简单的任务,DevExpress 是一团糟……

标签: c# .net winforms devexpress


【解决方案1】:

我发现实际上有一种删除按钮。所以,我按照问题中的方式做所有事情,但是我没有选择那种 Glyph,而是选择了 Delete,并且我不需要选择图像。

【讨论】:

    【解决方案2】:

    我已经总结了我在DevExpress forum找到的内容:

    使用ButtonEdit 控件并将TextEditStyle 属性设置为HideTextEditor。 Repository Item 有一个Buttons 集合,您可以通过它添加标题、图像等。

    Buttons 集合中,将“Kind”属性更改为“Glyph”。 您可以使用CustomRowCellEdit 事件有条件地逐个单元格地应用编辑器。 确保将 Button 的 Kind 属性设置为“字形”,并将 Caption 属性设置为您想要的任何文本:

    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;
    

    您应该处理 GridView 的CustomRowCellEdit 事件,构造一个新的RepositoryItemButtonEdit 并将其分配给e.RepositoryItem property

    让我知道这是否有效。

    【讨论】:

    • 是的,这行得通。谢谢您-我已将您的答案标记为有用。几个 cmets: 1. 只需将代码包装在 if(e.Column == [DeleteColumnName]) {...} 中,否则它将所有列变成按钮。 2. 我更喜欢我的答案(使用 Delete 类型而不是 Glyph),因为我认为使用内置 X 比键入“X”更整洁。我还没有弄清楚如何为 X 使用自定义图像,但这并不重要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多