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

[Solved] Is it possible to make certain Grid Columns Viewable but not Editable?

4 Answers 107 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Paul
Top achievements
Rank 1
Paul asked on 06 Sep 2010, 03:11 PM
Apologies if this question is asked elsewhere but I cannot find it.

I am using the latest V2 release 825

I have created a user defined model containing a couple of fields, one of which relates to the primary key of my database table and cannot be edited. However I still want to show this in the Grid, but I don't want users to be able to edit that particular column when editing within the grid as it will not be written to the database. How do I make this viewable only?

Thanks,
Paul

4 Answers, 1 is accepted

Sort by
0
Jigar
Top achievements
Rank 1
answered on 06 Sep 2010, 03:18 PM
Hi Paul,

I suggest to make that field read only.

For example:

Model:

Public Class TestModel
    <UIHint("CreatedDateTime")>
    Public Property CreatedDateTime As DateTime
End Class


Component:
<%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl(Of System.DateTime?)" %>
<%  Dim controlName = ViewData.TemplateInfo.GetFullHtmlFieldName(String.Empty) %>
<%= Html.TextBox(controlName, IIf(Model > DateTime.MinValue, Model, DateTime.Now), New Dictionary(Of String, Object) From {{"readonly", "readonly"}})%>


I hope this may help.

Regards,

Jigar.
0
Paul
Top achievements
Rank 1
answered on 06 Sep 2010, 03:37 PM
Hi,

Thanks, that was incredibly easy to do!!

Regards
Paul
0
Paul
Top achievements
Rank 1
answered on 06 Sep 2010, 11:36 PM
As my id field is a code, I need it to be editable on an Insert but not on an update.

Is there any way around this in the Grid as I'm currently making the id BoundColumn ReadOnly which means I cannot insert new rows.

Thanks
0
Paul
Top achievements
Rank 1
answered on 07 Sep 2010, 09:49 AM
I used this solution in case anyone else is looking for this:

http://www.telerik.com/community/forums/aspnet-mvc/grid/insert-only-field.aspx

Remove the ReadOnly Binding and this piece of script will hide your id field on an edit leaving it in place on an insert. Would be nice if there was a better way to do this in the code though, especially as any css class name updates in future would break the script.

Paul

Tags
Grid
Asked by
Paul
Top achievements
Rank 1
Answers by
Jigar
Top achievements
Rank 1
Paul
Top achievements
Rank 1
Share this question
or