This is a migrated thread and some comments may be shown as answers.

Read-only cell (simulation) and the behavior when that cell is left

4 Answers 92 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alain
Top achievements
Rank 1
Alain asked on 26 Jan 2015, 09:36 PM
Hi, I am using a RadGridView in which some of the cells might be read-only depending on the values of the data in other columns.
To simulate the read-only behavior, I use «CellBeginEdit» event has recommended in some threads of this forum.
My code looks like this:

private void radGridView_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
        {
            e.Cancel = !IsCellUpdatable();      
        }

The «IsCellUpdatable» function seen above returns true or false depending on if the cell has to be modified.
Up to that part, everything works properly.

The problems I encounter are:

1. Normally, when I leave a cell using the tab key the next cell is entered and it is in edit mode (I mean it has the focus and I can see the cursor).  This also works the same way if I leave a cell that is in a column that is read-only.  But when I leave a cell in which I simulate the read-only behavior the next cell is not entered in edit mode.  This different behavior confuse my users.

2. While adding a new row, normally if I leave the grid my new row (it's data) becomes a standard row and a new empty row is displayed as the new row.  But when I leave the grid while I am in a cell for which I simulate the read-only behavior, nothing will happen (the data will stay in the new row) and it looks like the new row has not been added to the list of rows.  Again, this behavior is really annoying for the users.

Can you please help me find a way to have read-only cells depending on the value of other columns while maintaining the same behavior for all the cells of the grid.

Thanks for your help,

Alain
 

4 Answers, 1 is accepted

Sort by
0
Accepted
Todor Vyagov
Telerik team
answered on 27 Jan 2015, 01:31 PM
Hello Alain,

Thank you for contacting Telerik support.

1. This happens, because the active editor is closed when you call e.Cancel = true. To open it again when the next editable cell is reached you can subscribe to the CurrentCellChanged event and call the RadListView BeginEdit method.
private void radGridView1_CurrentCellChanged(object sender, CurrentCellChangedEventArgs e)
{
    if (!radGridView1.IsInEditMode && e.CurrentCell != null && e.NewCell != null)
    {
        if (IsCellUpdatable())
        {
            radGridView1.BeginEdit();
        }
    }
}

2. To add new row to RadGridView you need to have at least one value in the new row. To set default value for read-only cells you can subscribe to the DefaultValuesNeeded event.
void radGridView1_DefaultValuesNeeded(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[0].Value = 1000;
}

More information about grid new rows can be found here: New Row 

I hope this information helps.

Regards,
Todor Vyagov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Alain
Top achievements
Rank 1
answered on 27 Jan 2015, 04:47 PM
Hi Todor, first I want to thank you for your fast answer.

Your solution to my first problem is great. It is exactly what I needed.

As for my second problem, I still need some help to fix the issue.  I can indeed create a new row.  The row contains many values and the read-only cells are initialized with values.  If I leave the row (or the grid) while the focus is on a cell that is updatable the new row becomes a standard row, everything is fine.  But, when I leave the row (or the grid) while the focus was on a cell where the read only behavior is simulated, then I get the problem.  The data is stuck in the new row instead of being transfered to a standard row.  
Thanks again, Alain


0
Accepted
Todor Vyagov
Telerik team
answered on 28 Jan 2015, 12:47 PM
Hello Alain,

Thank you for writing back.

This happens, because read-only cells are cancelling the active editor and new row is not in edit mode, so the new row is not validated and is not added to your RadGridView. To add the new row in this case you can subscribe to the RowValidated event and call new row's EndAddNewRow method.
void radGridView1_RowValidated(object sender, RowValidatedEventArgs e)
{
    GridViewNewRowInfo newRow = e.Row as GridViewNewRowInfo;
    if (newRow != null && !this.radGridView1.IsInEditMode && newRow.IsModified)
    {
        newRow.EndAddNewRow();
    }
}

I hope this information is useful.

Regards,
Todor Vyagov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Alain
Top achievements
Rank 1
answered on 29 Jan 2015, 06:50 PM
Hi Todor, your solution works great. Your help was very much appreciated.
Tags
GridView
Asked by
Alain
Top achievements
Rank 1
Answers by
Todor Vyagov
Telerik team
Alain
Top achievements
Rank 1
Share this question
or