[英]DataGridView Readonly “bug”
I have a datagridview
to display some data. 我有一个
datagridview
来显示一些数据。 Some rows between data are separator rows
so those are readonly
. 数据之间的某些行是
separator rows
因此这些separator rows
是readonly
。 In some cases the whole datagridview
might be readonly
. 在某些情况下,整个
datagridview
可能只是readonly
。 But when I switch it back to readonly = false
, all the rows are editable. 但是当我将其切换回
readonly = false
,所有行都是可编辑的。 Is it possible with out having to set again the readonly
property of each row manually that my row came back as they were before? 有可能不必手动重新设置每行的
readonly
属性,我的行回来了吗?
As far as I can see using Reflector, setting DataGridView.ReadOnly
to true
will also set ReadOnly
to false
for all rows and columns in the grid - presumably it is assumed that you'll never subsequently want to set DataGridView.ReadOnly
to false
again. 据我所知,使用Reflector,将
DataGridView.ReadOnly
设置为true
也会将网格中所有行和列的ReadOnly
设置为false
- 可能会假设您以后再也不想将DataGridView.ReadOnly
设置为false
。
So the only way I can see for you to get round this, is to "remember" which rows should be ReadOnly, for example by setting a suitable value in DataGridViewRow.Tag
, then using this to restore the ReadOnly
state manually. 所以我能看到你唯一的方法就是“记住”哪些行应该是ReadOnly,例如在
DataGridViewRow.Tag
设置一个合适的值,然后用它来手动恢复ReadOnly
状态。
For example, if you've set the DataGridViewRow.Tag
property to true
for readonly rows, you could handle the DataGridView.ReadOnlyChanged
event with a handler that looks something like the following untested code: 例如,如果您为只读行设置
DataGridViewRow.Tag
属性为true
,则可以使用类似于以下未测试代码的处理程序处理DataGridView.ReadOnlyChanged
事件:
void DataGridView_ReadOnlyChanged(object sender, EventArgs e)
{
DataGridView dataGridView = (DataGridView) sender;
if (!dataGridView.ReadOnly)
{
// DataGridView.ReadOnly has just been set to false, so we need to
// restore each row's readonly state.
foreach(DataGridViewRow row in dataGridView.Rows)
{
if (row.Tag != null && ((bool)row.Tag))
{
row.ReadOnly = true;
}
}
}
}
However it seems clear that the DataGridView
isn't designed to allow its ReadOnly
property to be toggled in that way. 但是很明显,
DataGridView
的设计不允许以这种方式切换其ReadOnly
属性。 Maybe you could design your application so that you don't ever need to set DataGridView.ReadOnly
to true
? 也许你可以设计你的应用程序,这样你就不需要将
DataGridView.ReadOnly
设置为true
?
For example, if you want to prevent a user from editing by double-clicking on a cell, you could set DataGridView.EditMode
to DataGridViewEditMode.EditProgramatically
instead of setting DataGridView.ReadOnly
to true
. 例如,如果要通过双击单元格来阻止用户进行编辑,可以将
DataGridView.EditMode
设置为DataGridViewEditMode.EditProgramatically
而不是将DataGridView.ReadOnly
设置为true
。
if the class is implemented by yourself then you can set your DataGrid1.ReadOnly = true
and make the properties ReadOnly which needs to be ReadOnly. 如果该类是由您自己实现的,那么您可以设置
DataGrid1.ReadOnly = true
并使属性ReadOnly需要是ReadOnly。
like this: 像这样:
string _myProperty;
public string MyProperty
{
get{return _myProperty;}
}
If you fill the DataGridView manually with code rather than binding it to a DataSource then you can simply set a row's readonly property to true when you add it. 如果使用代码手动填充DataGridView而不是将其绑定到DataSource,则可以在添加时将行的readonly属性设置为true。
If the above won't work then I don't understand what your code actually does, and like @Dilshod said it would be convenient if you posted it, or at least linked to a gist of it ( http://www.gist.github.com ). 如果以上内容不起作用,那么我不明白你的代码实际上做了什么,就像@Dilshod说如果发布它会很方便,或至少链接到它的要点( http://www.gist .github.com )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.