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

Set readonly property on the cell level

2 Answers 210 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Bob
Top achievements
Rank 1
Bob asked on 15 Nov 2010, 08:28 PM
Is it possible to set the readonly property of a single cell in a row or column?  I want certain cells to be readonly based on other data in the row.

Thanks

2 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 15 Nov 2010, 09:16 PM
Hi Bob, 
 
there are a few ways to do this. Two of which I have listed here.
the first is via the CellFormatting event.
Private Sub RadGridView1_CellFormatting(ByVal sender As System.Object, _
    ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles RadGridView1.CellFormatting
    If TypeOf e.CellElement.RowElement.RowInfo Is GridViewDataRowInfo Then
        If e.CellElement.RowElement.RowInfo.Cells("Currency").Value IsNot Nothing Then
            If e.CellElement.RowElement.RowInfo.Cells("Currency").Value.ToString() = "2.32" AndAlso e.Column.HeaderText = "MyDateTime" Then
                e.CellElement.Enabled = False
            End If
        End If
    End If
End Sub

However, I have a preference for catching when the cell is being edited as it will only fire when the user tries to edit it. If you don't want to apply any other formatting to the cell, I think this way is better.

Private Sub RadGridView1_CellBeginEdit(ByVal sender As System.Object, _
    ByVal e As Telerik.WinControls.UI.GridViewCellCancelEventArgs) Handles RadGridView1.CellBeginEdit
    If Me.RadGridView1.CurrentRow.Cells("Currency").Value.ToString() = "2.32" AndAlso Me.RadGridView1.CurrentCell.ColumnInfo.HeaderText = "MyDateTime" Then
        e.Cancel = True
    End If
End Sub

This last one is saying that if the value of the Currency cell in the current row is 2.32, and we are on the MyDateTime column, then don't allow editing of the MyDateTime cell.

Hope that helps, but let me know if you need more information

Richard
0
Bob
Top achievements
Rank 1
answered on 15 Nov 2010, 09:52 PM
Looks good.  I prefer option 2.
Tags
GridView
Asked by
Bob
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Bob
Top achievements
Rank 1
Share this question
or