【问题标题】:Allowing one column to be edited but not another允许编辑一列但不允许编辑另一列
【发布时间】:2011-10-04 21:26:52
【问题描述】:

我有一个 asp.net c# 应用程序。

我的 gridview 有一个包含 2 个字段的数据源。

用户无法编辑 1 个字段,但我需要另一个字段可编辑!

这可能吗?

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    ReadOnly="true" 属性设置为您不想编辑的所有内容。

    看看http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.readonly.aspx

    该页面的一个简单示例

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

    在这种情况下,CustomerIDCompany 名称是只读的,不能更改。 AddressCityPostalCode可以编辑。

    只需在您不希望人们编辑的列上将 ReadOnly 选项设置为 true。在编辑模式下,用户可以编辑未设置此设置或 ReadOnly 设置为 false 的列。

    【讨论】:

    【解决方案2】:

    如果您使用 SqlDataSource,则应确保从 SqlDataSource 中的 UpdateCommand 中删除要更新的列,否则在更新字段时会出现问题

    【讨论】:

      【解决方案3】:

      您应该能够在 DataGridViewCell 上设置 ReadOnly。

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

      gridView.Rows[rowIndex][colName].ReadOnly = true;
      

      【讨论】:

      • 添加了一个例子,并不是说它不是自我解释的。
      • 你能帮忙解决这个问题吗stackoverflow.com/questions/7655124/…
      • @I__ - 请使用左侧的勾号将您给出的答案之一标记为正确的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 2017-04-10
      • 2014-04-06
      相关资源
      最近更新 更多