How to set a GridView read-only line made with DevExpress?

2

I need to put the lines of my GridView as read-only. That is, I have a GridView that already contains some data, this data can not be edited, however I can add new lines, and these new lines can be edited.

But I'm using the XtraGrid component of DevExpress, version 16.1

private void button4_Click(object sender, EventArgs e) // botao editar
    {
        editar = 1;
        readonly_false();
        gridLookUpEdit1.ReadOnly = true;

        int linhas = gridView5.RowCount;

        for(int i = 0; i <= linhas; i++)
        {
           // codigo para colocar as linhas como readonly = true.               
        }
    }

Does anyone have any ideas?

    
asked by anonymous 05.09.2016 / 20:20

2 answers

2

I was able to resolve using the ShowingEditor event of GridView :

First, when the user clicks the "Edit" button we have:

private void button4_Click(object sender, EventArgs e) // botao editar
    {
        // defino editar = 1, para saber que esta em edição
        editar = 1;
        // método para deixar os textbox editaveis (ReadOnly = false)
        readonly_false();
        gridLookUpEdit1.ReadOnly = true;

        // conto quantas linhas existem no GridView 
        it = gridView5.RowCount;
    }

// metodo ligado ao evento ShowingEditor
private void cell_readonly(object sender, CancelEventArgs e)
    {
        if(gridView5.GetSelectedRows()[0] == -2147483647)
        {
            // ocorre quando clicado para adicionar nova linha
        }
        else if (gridView5.GetSelectedRows()[0] < it)
        {
            // ocorre nas linhas que já existiam.

            // cancelo a edição nas colunas com nomes entre ""
            e.Cancel = gridView5.FocusedColumn.FieldName == "DESCRIÇÃO" || gridView5.FocusedColumn.FieldName == "QUANTIDADE"
                       || gridView5.FocusedColumn.FieldName == "OF ORIGEM";
        }
        else
        {
            // ocorre nas linhas que foram adicionadas
        }
    }
    
06.09.2016 / 12:51
0

For GridView from DevExpress 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;
}

For .NET.

The ReadOnly indicates whether the data displayed by the cell can be edited. You can set ReadOnly to individual cells, or you can make an entire row or column of read-only cells by setting the DataGridViewRow.ReadOnly or DataGridViewColumn.ReadOnly properties. By default, if the parent row of a cell or column is set to read-only, the child cells will adopt the same value. You can override this default behavior by setting ReadOnly to individual cells. You can navigate to a read-only cell, and you can set a read-only cell to be the current cell. ReadOnly only affects whether a cell is editable; it does not affect whether the user can delete rows.

In your loop do it.

for(int i = 0; i <= linhas; i++)
{
   gridView5.Rows[i].Cells["colName"].ReadOnly = true;             
}
    
05.09.2016 / 20:31