Set "Column.ReadOnly = True" on new row being added by the user

5 posts, 0 answers
  1. Alex
    Alex avatar
    7 posts
    Member since:
    Nov 2014

    Posted 21 May 2015 Link to this post

    Hello,

    I have a small form with an editable grid on it (databound to an ORM entity), and although I allow the users to edit most of the columns in a giving row, I would like to lock a few columns on the new row (when they click on "Click here to add a new row"). I have been trying to use gridview_CellFormatting, but I can't seem to figure out how to identify whether or not the column in context belongs to the new row (being created) or an already existing one on the grid. 

    Any help would be truly appreciated.

     Thanks!

     

     

     

  2. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 22 May 2015 Link to this post

    Hi Alex,

    Thank you for writing.

    The CellFormatting event is generally used for styling the cells. In addition if you set the ReadOnly property of a specific column the user would not be able to type value for this column in the new row as well. Nevertheless you can change the styles for a cell in the new row like this:
    void radGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
    {
        if (e.Row is GridViewNewRowInfo && e.Column.ReadOnly)
        {
            e.CellElement.DrawFill = true;
            e.CellElement.BackColor = Color.Red;
        }
        else
        {
            e.CellElement.ResetValue(LightVisualElement.DrawFillProperty);
            e.CellElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
        }
    }

    Please let me know if there is something else I can help you with. 
     
    Regards,
    Dimitar
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Alex
    Alex avatar
    7 posts
    Member since:
    Nov 2014

    Posted 22 May 2015 in reply to Dimitar Link to this post

    Hello Dimitar and thanks for your reply.

    Actually, I am not looking to set a style, I am actually looking to set the column itself to read-only. Something like this:

     

    Private Sub gv_CellFormatting(sender As Object, e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles gv.CellFormatting
     
        If e.Row.Index = -1 Then
     
            Select Case e.Column.Name
                Case "A"
                    e.Column.ReadOnly = False
                Case "B"
                    e.Column.ReadOnly = True
            End Select
             
        End If
     
    End Sub

     

    The problem is that everything I try here, gets applied to all rows, new or existing. I have also tried using GridViewNewRowInfo like this:

    If TypeOf (e.CellElement.RowElement) Is Telerik.WinControls.UI.GridNewRowElement Then
        'Code here
    End If
     

    What I am trying to accomplish is simple. The grid in editable, and users can edit columns A though D. However, when they add a new row, I want to lock column A, because I will set the value myself through code before the data gets committed to the database.

     

    Thanks again.

     

     

     

     

  5. Alex
    Alex avatar
    7 posts
    Member since:
    Nov 2014

    Posted 22 May 2015 in reply to Alex Link to this post

    Maybe this should be done in another event, instead of _CellFormatting?
  6. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 25 May 2015 Link to this post

    Hello Alex,

    Thank you for writing back.

    This should be done in the CellBeginEdit event. For example:
    Private Sub radGridView1_CellBeginEdit(ByVal sender As Object, ByVal e As GridViewCellCancelEventArgs)
        If TypeOf e.Row Is GridViewNewRowInfo AndAlso e.ColumnIndex = 0 Then
            e.Cancel = True
        End If
     
    End Sub

    Please let me know if there is something else I can help you with. 
     
    Regards,
    Dimitar
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for WinForms is Visual Studio 2017 Ready