问题
How can I make a specific row cell readonly(not editable) on XtraGrid? For example just for row[0] but not all rows.
回答1:
You can use the GridView.CustomRowCellEdit event:
//...
var repositoryItemTextEditReadOnly = new DevExpress.XtraEditors.Repository.RepositoryItemTextEdit();
repositoryItemTextEditReadOnly.Name = "repositoryItemTextEditReadOnly";
repositoryItemTextEditReadOnly.ReadOnly = true;
//...
void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e) {
if(e.RowHandle == 0)
e.RepositoryItem = repositoryItemTextEditReadOnly;
}
回答2:
You can use the ColumnView.ShownEditor event:
void gridView1_ShownEditor(object sender, EventArgs e)
{
ColumnView view = (ColumnView)sender;
view.ActiveEditor.Properties.ReadOnly = view.FocusedRowHandle == 0;
}
回答3:
Source: How to Conditionally Prevent Editing for Individual Grid Cells
When you need to make a grid cell read-only based on a condition, the best approach is to use the ShowingEditor event of the GridView and prevent editing via the e.Cancel parameter passed to the event. Simply set it to True when it is necessary to prevent editing.
// disable editing
private void gridView1_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e) {
GridView view = sender as GridView;
e.Cancel = view.FocusedRowHandle == 0;
}
Source - How to display disabled buttons for particular cells within a ButtonEdit column
Another approach is that assign a read only repository editor control as @DmitryG suggested and I have also implement that way some times when there was a column which contains a button.
In your case you should create two TextEdit repository items. One with the enabled button and another with the disabled button. Then handle the GridView.CustomRowCellEdit event and pass the necessary repository item to the e.RepositoryItem parameter according to a specific condition. Please see the Assigning Editors to Individual Cells help topic for additional information.
private void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
{
if (e.Column.Caption == "Any2")
{
if (e.RowHandle == 0)
e.RepositoryItem = columnReadOnlyTextEdit;
else
e.RepositoryItem = columnTextEdit;
}
}
References:
How to customize the Look-And-Feel of my grid cells
How to make my grid columns read-only
来源:https://stackoverflow.com/questions/14011027/can-i-make-row-cell-value-readonly-on-xtragrid-just-for-one-row