Gridview disable edit on 1 column asp.net

一笑奈何 提交于 2019-12-22 04:15:11

问题


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


回答1:


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>



回答2:


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



回答3:


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 :)




回答4:


If you are using template field

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

if boundfield

((BoundField)gvGridView.Columns[index]).ReadOnly = true;


来源:https://stackoverflow.com/questions/7171530/gridview-disable-edit-on-1-column-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!