read only grid but need insert/delete row

11 posts, 0 answers
  1. Jim
    Jim avatar
    68 posts
    Member since:
    Apr 2011

    Posted 09 Dec 2011 Link to this post

    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

  2. Tyree
    Tyree avatar
    105 posts
    Member since:
    Dec 2006

    Posted 09 Dec 2011 Link to this post

    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.
  3. Jim
    Jim avatar
    68 posts
    Member since:
    Apr 2011

    Posted 09 Dec 2011 Link to this post

    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
  4. Jim
    Jim avatar
    68 posts
    Member since:
    Apr 2011

    Posted 12 Dec 2011 Link to this post

    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

     

     

     

     

  5. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 13 Dec 2011 Link to this post

    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 >>

  6. Jim
    Jim avatar
    68 posts
    Member since:
    Apr 2011

    Posted 13 Dec 2011 Link to this post

    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
  7. Jim
    Jim avatar
    68 posts
    Member since:
    Apr 2011

    Posted 15 Dec 2011 Link to this post

    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







  8. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 15 Dec 2011 Link to this post

    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 >>

  9. Tyree
    Tyree avatar
    105 posts
    Member since:
    Dec 2006

    Posted 15 Dec 2011 Link to this post

    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.
  10. Jim
    Jim avatar
    68 posts
    Member since:
    Apr 2011

    Posted 15 Dec 2011 Link to this post

    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
  11. Tyree
    Tyree avatar
    105 posts
    Member since:
    Dec 2006

    Posted 15 Dec 2011 Link to this post

    Ahh, that makes it a bit more straight forward. :) GL
Back to Top