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

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

4 Answers 459 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 21 May 2015, 08:08 PM

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!

 

 

 

4 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 22 May 2015, 12:43 PM
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
0
Alex
Top achievements
Rank 1
answered on 22 May 2015, 01:04 PM

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.

 

 

 

 

0
Alex
Top achievements
Rank 1
answered on 22 May 2015, 01:13 PM
Maybe this should be done in another event, instead of _CellFormatting?
0
Dimitar
Telerik team
answered on 25 May 2015, 11:08 AM
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
Tags
GridView
Asked by
Alex
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Alex
Top achievements
Rank 1
Share this question
or