How to disable some cells in DataGrid?
How to disable some cells in DataGrid?
I found lot of answers on Stack Overflow how to disable specific cell in DataGrid in Windows Forms or WPF. Now I want to ask same question in DevExpress. Thank you for your answers!
My current somehow working code prevent user to check specific checkbox in grid but this checkbox dosen't look like it is disabled. How can I visually disable this field making it gray or none visible at all?
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;
}
}
Can you paste simple code about the appearance control?
– Piotr Wojcik
Jul 3 at 9:37
1 Answer
1
Use GridView.ShowingEditor and GridView.CustomDrawCell to do what you're after. See:
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;
}
}
If you would like to not show a checkbox at all, you can do this:
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;
}
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You should use GridView.ShowingEditor to disable the user from editing the cell, and you can use GridView.CustomDrawCell to control its appearance.
– MineR
Jul 3 at 9:29