c# - 如何禁用 DataGrid 中的某些单元格?
问题描述
我在 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 来做你想做的事。看:
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;
}
}
推荐阅读
- python - 使用 Pythorch 翻转 2 张量蒙版
- python - 负 SKlearn 线性回归分数
- reactjs - 如何使用多个选择的选项 React
- java - 在 for 循环中侦听鼠标单击事件,并在以后的代码中使用该事件的坐标
- google-apps-script - 使用 Google Apps 脚本将项目符号列表插入 Google Doc 的首选方法是什么?
- node.js - 如何在我的 socket.io 项目中添加多个房间?
- laravel - firstOrCreate 是否尊重演员表?
- rust - 使用 std::iter::IntoIterator 的代码不起作用
- chef-infra - 在本地机器上使用 Packer 配置 Chef
- python - 使用 pandas 数据框列作为键创建嵌套字典的有效方法