将ReadOnly设置为false,但仍然有“ReadOnlyException:列X只读” [英] Set ReadOnly to false but still got "ReadOnlyException: Column X is read only"
问题描述
组件: DataGridView
案例:我有一个 DataGridView
,它使用 DataSource
从数据库填充。另一方面,我有一个表单
用于查看和编辑一个数据行。成功更新后,我想直接在 DataGridView
中更改一行的数据 - 使用以下代码:
int rowIndex = 0;
DataGridViewRow row = dtgMasterAsset.Rows [rowIndex];
row.Cells [1] .Value =新资产名称; // 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;
只是为了使它肯定再次。
但错误仍然出现。为什么?
任何解释?
谢谢。
FYI:所有代码都在
刚刚发现了罪魁祸首。
这是我填充 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;
不知何故$ code DataGridView 阻止系统重新设置/设置从别名SQL查询列填充的列( SQL Table Alias )。要修复它,只需将列绑定到另一个非别名列。
其他方式?我不知道:(
I am new to C# and maybe similar question were exist but its different.
Component: DataGridView
Case: I have a DataGridView
that is populated from a database using DataSource
. On the other side I have a Form
that's used for viewing and editing a row of data. After it is successfully updated I want to change a row's data in the DataGridView
directly - using this code:
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
In the process, Cells[1]
is successfully updated but not so with Cells[2]
. An error message pops up saying:
Column ITEM_CATEGORY_NAME is read only
I try to change the ITEM_CATEGORY_NAME
column to Readonly = false
in the designer and added a line row.Cells[2].Readonly= false;
just to make it sure AGAIN.
But the error still comes up. Why? Any explanation?
Thank you.
FYI: All code is on the same class.
Just found the culprit.
This is how i populate the 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;
Somehow the DataGridView
blocks the system to re-set/set the column that is populated from Aliased SQL Query column (SQL Table Alias). To fix it just bind the column to another non aliased column.
Other way? i don't know :(
这篇关于将ReadOnly设置为false,但仍然有“ReadOnlyException:列X只读”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!