Can I make row cell value readOnly on XtraGrid jus

2019-06-22 09:33发布

How can I make a specific row cell readonly(not editable) on XtraGrid? For example just for row[0] but not all rows.

3条回答
女痞
2楼-- · 2019-06-22 09:37

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.

enter image description here

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

查看更多
ら.Afraid
3楼-- · 2019-06-22 09:39

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;
}
查看更多
贪生不怕死
4楼-- · 2019-06-22 09:55

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;
}
查看更多
登录 后发表回答