8

I am using a gridview Edit to edit the values i have in my gridview, when i press edit, all columns can be edited, i would like that one of the columns is not allowed to be edited.

Is there any way i can do this?

Thiss is my aspx code:

<asp:GridView 

ID="GridView1" 
runat="server" 
AllowSorting="True" 
OnRowCommand="GridView1_SelectedIndexChanged1" 
AutoGenerateEditButton="True" 
OnRowCancelingEdit="GridView1_RowCancelingEdit" 
CellSpacing="10"
OnRowUpdating="GridView1_RowUpdating" ShowFooter="True" 
onselectedindexchanged="GridView1_SelectedIndexChanged"
OnRowEditing="GridView1_RowEditing">



</asp:GridView>

Thanks

Karl
  • 781
  • 2
  • 9
  • 26

4 Answers4

28

If you are using asp:BoundField, try

<asp:BoundField DataField="CustomerID"
        ReadOnly="true"      
        HeaderText="Customer ID"/>

Or else if you are using asp:TemplateField, you can either

  1. Render it in either an asp:Label inside EditItemTemplate
  2. Omit the EditItemTemplate for that column altogether
naveen
  • 53,448
  • 46
  • 161
  • 251
  • i am using databind, im trying to use your boundfield but its telling me that its not found – Karl Aug 24 '11 at 07:08
  • 2
    see this example. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.aspx BoundField must be inside `Columns` – naveen Aug 24 '11 at 07:15
  • 1
    This is the best solution if you are using boundfields. And if possible, put the non editable fields in the BoundField. – MindLoggedOut Mar 13 '13 at 14:58
7

Sure, make use of the EditItemTemplate. In the following example field ID will not be edited in the Edit mode:

<asp:GridView runat="server">
    <Columns>
        ...
        <asp:TemplateField HeaderText="ID">
            <EditItemTemplate>
                <%# Eval("ID") %>
            </EditItemTemplate>
        </asp:TemplateField>
        ...
    </Columns>
</asp:GridView>
Andrei
  • 55,890
  • 9
  • 87
  • 108
  • this works, but it creates another column and does not make the current one read only – Karl Aug 24 '11 at 07:09
  • 2
    That's because by default GridView's property `AutoGenerateColumns` is set to true. So it generates columns for every property of the object that is provided as a DataItem and adds the declared column (ID in our case). There is no way you can affect the behavior of auto generated columns. If you want something custom (like edit disabling on a certain column) you should switch off auto generation of columns and declare everything you need using BoundField, TemplateField etc. – Andrei Aug 24 '11 at 07:18
0

please show some markup. Quick and dirty I think depending on your markup aspx you can remove the textbox from the EditItem template for the column you want to prevent editing... there are also other solutions of course :)

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
0

If you are using template field

((TemplateField)gvGridView.Columns[index]).EditItemTemplate = null;

if boundfield

((BoundField)gvGridView.Columns[index]).ReadOnly = true;
Denys Wessels
  • 16,829
  • 14
  • 80
  • 120
rofans91
  • 2,970
  • 11
  • 45
  • 60