【问题标题】:Custom caption next to CheckEdit Editor in XtraGridXtraGrid 中 CheckEdit 编辑器旁边的自定义标题
【发布时间】:2014-12-28 18:22:41
【问题描述】:

我有一个绑定到数据源的 XtraGrid,我希望在其中的单个列中有一个复选框,并在同一单元格中的复选框旁边有一个标题。我可以从数据源中的另一列获取 Checked True 或 False,并且我还需要从 Datasource 中的另一列获取标题文本......一旦我掌握了实现标题更改的句柄,这部分很容易。

我的问题是:

如何以编程方式更改同一单元格中 CheckEdit 旁边显示的文本?

类似于(Tick 和 Untick 代表复选框状态):

Tick  Apples
Untick Bananas

但是,我的尝试只显示文本“Check”——显然是 CheckEdit 编辑器中的默认标题:

Tick Check
Untick Check

我搜索了 DevExpress 支持中心,他们只有在 TreeList(而不是 XtraGrid)中使用 CheckEdit Repository 项时的说明。我知道这将涉及管理 CustomDrawCell 事件和其他一些事件 - 这很好。

【问题讨论】:

    标签: winforms devexpress xtragrid


    【解决方案1】:

    我认为,您对XtraTreeList 的指示也可以应用于XtraGrid。但我可以向你建议另一种方式。
    您可以使用GridView.CustomRowCellEdit 事件和两个ResositoryItemCheckEdit 对象:

    private RepositoryItemCheckEdit editTick;
    private RepositoryItemCheckEdit editUntick;
    

    只需将Tick 文本设置为editTick.Caption 属性,将Untick 文本设置为editUntick.Caption 属性。
    这是一个例子:

    //Initialize repository items:
    editTick = new RepositoryItemCheckEdit() { GlyphAlignment = HorzAlignment.Near, Caption = "Apples" };
    editUntick = new RepositoryItemCheckEdit() { GlyphAlignment = HorzAlignment.Near, Caption = "Bananas" };
    
    //Add handler for CheckedChanged event:
    Action<object, EventArgs> action = (s, e) =>
    {
        var ownerEdit = s as CheckEdit;
    
        if (ownerEdit == null)
            return;
    
        ownerEdit.Text = ownerEdit.Checked ? editTick.Caption : editUntick.Caption;
    };
    
    editTick.CheckedChanged += new EventHandler(action);
    editUntick.CheckedChanged += new EventHandler(action);
    
    //Some sample DataSource:
    var table = new DataTable();
    
    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Bool", typeof(bool));
    
    table.Rows.Add(0, false);
    table.Rows.Add(1, false);
    table.Rows.Add(2, true);
    table.Rows.Add(3, false);
    table.Rows.Add(4, true);
    table.Rows.Add(5, true);
    table.Rows.Add(6, false);
    table.Rows.Add(7, true);
    
    gridControl.DataSource = table;
    
    //Here comes the CustomRowCellEdit:
    gridView1.CustomRowCellEdit += (s, e) =>
    {
        if (e.Column.FieldName != "Bool" || e.CellValue == null) //Put your own field name here instead of "Bool".
            return;
    
        e.RepositoryItem = (bool)e.CellValue ? editTick : editUntick;
    };
    
    //Add CustomColumnDisplayText event, so you can see your "Apples" in group rows and filters:
    gridView1.CustomColumnDisplayText += (s, e) =>
    {
        if (e.Column.FieldName != "Bool")
            return;
    
        e.DisplayText = (bool)e.Value ? editTick.Caption : editUntick.Caption;
    };
    

    【讨论】:

    • 感谢您的回复,给我一点时间来处理它(我前面有很长的车程)
    • 我尝试了 CustomColumnDisplayText,不幸的是这不适用于存储库项目。我只有大约 15 行,所以我所做的是创建 CheckEdit 编辑器的 15 个存储库项(在排序字典中使用来自表另一列中唯一 ID 的键)。然后我为每个代表项目设置标题(以及文本颜色等,因为我需要一些红色项目,并为每个代表项目添加事件处理程序),我使用 CustomRowCellEdit 分配适当的代表项目。为您的回答 +1,因为它很有帮助,非常感谢!
    • @Grantly CustomColumnDisplayText 事件为在基于Text Box 的单元格、分组行和过滤器下拉列表中显示的列值提供自定义显示文本。但此事件不适用于基于 Check Box 的单元格。
    猜你喜欢
    • 2016-07-25
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 2020-12-23
    • 2012-11-08
    • 2014-09-16
    • 2015-11-09
    • 1970-01-01
    相关资源
    最近更新 更多