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

read only grid but need insert/delete row

10 Answers 480 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jim
Top achievements
Rank 1
Jim asked on 09 Dec 2011, 12:18 PM
I have a grid and don't want the existing contents editable.
But want to allow insert/delete. ( where you can enter data into the insert )

I was looking at some post talking about BeginningEdit  and Cancel.
But in BeginningEdit can't determine if grid is in Insert or Edit mode.

Any ideas on the best approach.
Thanks
Jim

10 Answers, 1 is accepted

Sort by
0
Tyree
Top achievements
Rank 2
answered on 09 Dec 2011, 01:58 PM
The columns and grid have a property "IsReadOnlyBinding" which gets evaluated when it attempts to put a cell into edit mode. This is evaluated with the row item as the context. I use this to determine what is visually editable. I also do all manipulation via the ViewModel so an "Add" creates a new object and adds it to the underlying collection (which in turn the grid magically shows), this object knows it is new and has not been commited to the datasource so if it had a property of IsReadOnly which is based on whether the object is new or not you can bind it to the grid.

<telerik:RadGridView IsReadOnlyBinding={Binding IsReadOnly}> would make each row as a whole readonly or not. If you used the columns instead you can narrow it to just certain columns - which doesnt sound like you care about.
0
Jim
Top achievements
Rank 1
answered on 09 Dec 2011, 02:38 PM
Thanks
I am not sure I want to work on the view model.
Just have a read only grid that you can delete/insert rows.

Jim
0
Jim
Top achievements
Rank 1
answered on 12 Dec 2011, 04:47 PM
Still not solved this.

The BeginningEdit event has a e.Cancel property.

 

So I could cancel the edit if I knew it was an not an insert.

Now the RowEditEnded event has an e.EditOperationType Insert/Edit.
But that's not available in BeginningEdit

Any way to detemine in BeginningEdit  ??

Help
Thanks
Jim

 

 

 

 

0
Dimitrina
Telerik team
answered on 13 Dec 2011, 10:21 AM
Hello Jim,

 If you would like to have a ReadOnly GridView that allows Insert and Delete, then you could set all your columns to be ReadOnly.

If this is not acceptable for you, then you could set your bound properties to be not editable.

[Editable(false, AllowInitialValue=true)]
public string Name
{
...
}

Do any of those solutions work for your case?

Greetings,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Jim
Top achievements
Rank 1
answered on 13 Dec 2011, 11:14 AM
I don't want to use the viewmodel method as it's auto generated (ish).

I tried setting the column to isreadonly but unfortunatly when you insert a column you can't edit the contents in insert mode.

Thanks
Jim
0
Jim
Top achievements
Rank 1
answered on 15 Dec 2011, 09:41 AM
Woke up in the middle of the night with an idea!
It seems to work ,,,
From previous work I know a new row has index -1
Hence the code for beginingedit is
    Dim rgv As RadGridView = DirectCast(sender, RadGridView)
Dim rowindex As Integer = rgv.Items.IndexOf(e.Row.Item)
   If rowindex > -1 Then
   e.Cancel = True
   End If


And then you have a grid that can't be editied but you can still
1) Delete rows.
2) Insert a new row and edit this row on insertion.

Any problems with this approach ?
Thanks
Jim







0
Dimitrina
Telerik team
answered on 15 Dec 2011, 12:58 PM
Hello Jim,

 Actually this code will only work when your ItemsSource is not an IList or when the ShowInsertRow=True.

You could better do it another way.
1. You will need your private variable that holds the state of the edit.

private bool isNewItemAdded= false;

2. Then on AddingNewDataItem event raised mark this bool flag:
private void clubsGrid_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
       {
           isNewItemAdded = true;
       }

3. On BeginningEdit check the flag:
private void clubsGrid_BeginningEdit(object sender, GridViewBeginningEditRoutedEventArgs e)
       {
           if (!isNewItemAdded)
           {
               e.Cancel = true;
               isNewItemAdded = false;
           }
       }

Please check this solution and let me know how it works for you.

All the best,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Tyree
Top achievements
Rank 2
answered on 15 Dec 2011, 02:05 PM
That would let you edit any item once you added a new one.

There are so many possible holes in either method without knowing more of the requirements. Are these "new" items always immediately commited and no longer editable? What happens if they fail validation? Can you "start" more than one new item and are they all editable until some external commit event?

You are going to have to maintain context if you insist on not wrapping your items in a vm. Does the AddingNewDataItem event give you the item it just created? If so you would be better served just storing that in a nullable object and then reseting the object to null after you can no longer edit that item. Then in your beginning edit check if the edit belongs to the item you have stored (or if its null)...if so edit it, if not cancel.
0
Jim
Top achievements
Rank 1
answered on 15 Dec 2011, 02:16 PM
It's a single column grid with just one Date column.
As for validation, when you press insert and go into the cell, edit it won't allow you to insert an invalid date.

So from my point of view it all looks OK.

But thanks for the different options I may need to look at those in future.

Jim
0
Tyree
Top achievements
Rank 2
answered on 15 Dec 2011, 02:28 PM
Ahh, that makes it a bit more straight forward. :) GL
Tags
GridView
Asked by
Jim
Top achievements
Rank 1
Answers by
Tyree
Top achievements
Rank 2
Jim
Top achievements
Rank 1
Dimitrina
Telerik team
Share this question
or