我是C#的新手,也许存在类似的问题,但它有所不同。
组件:DataGridView
案例:我使用DataGridView
从数据库填充DataSource
。另一方面,我有一个Form
用于查看和修改一个数据行。成功更新后,我想直接在DataGridView
中更改行的数据 - 使用以下代码:
int rowIndex= 0;
DataGridViewRow row= dtgMasterAsset.Rows[rowIndex];
row.Cells[1].Value = "NEW ASSET NAME"; // ITEM_NAME
row.Cells[2].Value = "NEW CATEGORY NAME"; // ITEM_CATEGORY_NAME
在此过程中,Cells[1]
已成功更新,但Cells[2]
未成功更新。弹出错误消息说:
列ITEM_CATEGORY_NAME仅供参考
我尝试在设计器中将ITEM_CATEGORY_NAME
列更改为 Readonly = false
,并添加一行 row.Cells[2].Readonly= false;
确定再次。
但错误仍然存在。为什么? 有什么解释吗?
谢谢。
仅供参考:所有代码都在同一个类中。
答案 0 :(得分:1)
首先检查整个DataGridView
的{{1}} - 属性是否为ReadOnly
:
false
然后检查所使用的列'dtgMasterAsset.ReadOnly = false;
- 属性是否也是ReadOnly
。
答案 1 :(得分:1)
正如Ashton指出的那样,您可以检查dtgMasterAsset.ReadOnly
是否为true
。
然而,你说:
Cells[1]
已成功更新
因此,dtgMasterAsset.ReadOnly
必须为false
。另外,你说你试过了:
row.Cells[2].ReadOnly = false; row.Cells[2].Value = "NEW CATEGORY NAME";
失败的唯一方法是row.Cells[2].ReadOnly
被dtgMasterAsset.Columns[2].ReadOnly
true
覆盖ITEM_CATEGORY_NAME
。正如错误所述,问题是列。
尽管你设置了:
Readonly = false
列到dtgMasterAsset.Columns[2].ReadOnly
我认为您应该在设置false
之前检查row.Cells[2].Value
是否为ReadOnly
,以确保该列确实可写。
我的想法是 - 也许你在设计师中的设置是以下一个(或多个):
<dx:GridViewDataTextColumn FieldName="InventoryBank" Caption="InventoryBank" Width="100px">
<EditItemTemplate>
<dx:ASPxSpinEdit runat="server" NumberType="Float" DecimalPlaces="1" AllowNull="true" AllowMouseWheel="true" AllowUserInput="true" MinValue="-999999999" ID="lblInventoryBank" Value='<%# Bind("InventoryBank") %>' Width="100px" Increment="1" MaxValue="999999999">
<ClientSideEvents LostFocus="fnTruncateInt" />
</dx:ASPxSpinEdit>
</EditItemTemplate>
</dx:GridViewDataTextColumn>
。答案 2 :(得分:1)
刚刚找到了罪魁祸首。
这就是我填充DataGridView
String query= "SELECT * FROM VW_ASSET_LIST "; // SQL View
SqlCommand queryCommand= new SqlCommand(query, connection);
SqlDataReader resultsSet= queryCommand.ExecuteReader();
DataTable dtAsset = new DataTable();
dtAsset.Load(resultsSet);
dtgMasterAsset.DataSource = dtAsset;
以某种方式DataGridView
阻止系统重新设置/设置从别名SQL查询列(SQL Table Alias)填充的列。要修复它,只需将列绑定到另一个非别名列。
另一种方式?我不知道:(
答案 3 :(得分:0)
从Datatable中读取数据后,您将该列称为只读列,请尝试执行此操作:
dt.Columns[0].ReadOnly = false;
注意:dt是数据表中的对象,数据表中的列数为0。
答案 4 :(得分:-1)
如果您使用DataGridView
填充SqlDataReader
,它将是只读的。
尝试使用SqlDataAdapter
。