【发布时间】:2011-10-04 21:26:52
【问题描述】:
我有一个 asp.net c# 应用程序。
我的 gridview 有一个包含 2 个字段的数据源。
用户无法编辑 1 个字段,但我需要另一个字段可编辑!
这可能吗?
【问题讨论】:
我有一个 asp.net c# 应用程序。
我的 gridview 有一个包含 2 个字段的数据源。
用户无法编辑 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>
在这种情况下,CustomerID 和 Company 名称是只读的,不能更改。 Address、City和PostalCode可以编辑。
只需在您不希望人们编辑的列上将 ReadOnly 选项设置为 true。在编辑模式下,用户可以编辑未设置此设置或 ReadOnly 设置为 false 的列。
【讨论】:
如果您使用 SqlDataSource,则应确保从 SqlDataSource 中的 UpdateCommand 中删除要更新的列,否则在更新字段时会出现问题
【讨论】:
您应该能够在 DataGridViewCell 上设置 ReadOnly。
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.readonly.aspx
gridView.Rows[rowIndex][colName].ReadOnly = true;
【讨论】: