【问题标题】:How to disable some cells in DataGrid?如何禁用 DataGrid 中的某些单元格?
【发布时间】:2018-07-03 09:09:08
【问题描述】:

我在 Stack Overflow 如何在 Windows 窗体或 WPF 中禁用 DataGrid 中的特定单元格上找到了很多答案。现在我想在 DevExpress 中问同样的问题。感谢您的回答!

我当前以某种方式工作的代码阻止用户检查网格中的特定复选框,但该复选框看起来并没有被禁用。如何在视觉上禁用此字段,使其变为灰色或根本不可见?

bool expression = ... // some expresssion

private void grid_ShownEditor(object sender, EventArgs e)
{
    GridView view  sender as GridView;
    if(view.FocusedColumn.FieldName == "specific column name with checkbox cells")
    {
        var row = view.GetRow(view.FocusedRowHandle);
        view.ActiveEditor.Enabled = expression;
    }
}

【问题讨论】:

  • 你应该使用 GridView.ShowingEditor 来禁止用户编辑单元格,你可以使用 GridView.CustomDrawCell 来控制它的外观。
  • 能贴一下外观控件的简单代码吗?

标签: c# datagridview datagrid devexpress


【解决方案1】:

使用 GridView.ShowingEditor 和 GridView.CustomDrawCell 来做你想做的事。见:

private bool isDisabled = false;

private bool IsDisabled(int row, GridColumn col)
{
    if (col.FieldName == "somename")
        return isDisabled;
    return false;
}

private void GridView_ShowingEditor(object sender, CancelEventArgs e)
{
    var gv = sender as GridView;
    e.Cancel = IsDisabled(gv.FocusedRowHandle, gv.FocusedColumn);
}

private void GridView_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e)
{
    if(IsDisabled(e.RowHandle, e.Column))
    {
        e.Appearance.BackColor = Color.Gray;
        e.Appearance.Options.UseBackColor = true;
    }
}

如果您根本不想显示复选框,您可以这样做:

private static RepositoryItemTextEdit _nullEdit;
public static RepositoryItemTextEdit NullEdit
{
    get
    {
        if (_nullEdit == null)
        {

            _nullEdit = new RepositoryItemTextEdit();
            _nullEdit.ReadOnly = true;
            _nullEdit.AllowFocused = false;
            _nullEdit.CustomDisplayText += (sender, args) => args.DisplayText = "";
        }
        return _nullEdit;
    }
}

private void GridView_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
{
    if(IsDisabled(e.RowHandle,e.Column))
    {
        e.RepositoryItem = NullEdit;
    }
}

【讨论】:

    猜你喜欢
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 2010-09-10
    • 2016-11-15
    • 1970-01-01
    • 2017-07-25
    相关资源
    最近更新 更多