Allowing one column to be edited but not another

前端 未结 3 593
灰色年华
灰色年华 2020-12-11 05:11

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 editab

相关标签:
3条回答
  • 2020-12-11 05:27

    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.

    0 讨论(0)
  • 2020-12-11 05:30

    You should be able to set ReadOnly on DataGridViewCell.

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.readonly.aspx

    gridView.Rows[rowIndex][colName].ReadOnly = true;
    
    0 讨论(0)
  • 2020-12-11 05:41

    If you use a SqlDataSource then you should make sure to remove the columns to update from the UpdateCommand in the SqlDataSource if not you will have a problem when you update the fields

    0 讨论(0)
提交回复
热议问题