Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

c# - Allowing one column to be edited but not another

i have an asp.net c# application.

my gridview has a datasource that has 2 fields.

1 field cannot be edited by the user, but i need the other one to be editable!

is this possible to do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Set the ReadOnly="true" property on all that you don't want editable.

Take a look at http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.readonly.aspx

A quick example from that page

<asp:gridview id="CustomersGridView" 
    datasourceid="CustomersSqlDataSource" 
    autogeneratecolumns="false"
    autogenerateeditbutton="true"
    allowpaging="true" 
    datakeynames="CustomerID"  
    runat="server">

    <columns>
      <asp:boundfield datafield="CustomerID" readonly="true" headertext="Customer ID"/>
      <asp:boundfield datafield="CompanyName" readonly="true" headertext="Customer Name"/>
      <asp:boundfield datafield="Address" headertext="Address"/>
      <asp:boundfield datafield="City" headertext="City"/>
      <asp:boundfield datafield="PostalCode" headertext="ZIP Code"/>
    </columns>
</asp:gridview>

In this case CustomerID and Company name are read only and can't change. Address, City and PostalCode can be edited.

Just set the ReadOnly option to true on the columns you DON'T want people to edit. The columns that don't have this set or have ReadOnly set to false are editable by the user when in edit mode.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...