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
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.
Dimitar
Telerik
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.
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.
Dimitar
Telerik