C# 以编程方式更改 DataGridView 行上的只读模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13996412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:19:22  来源:igfitidea点击:

Programmatically Changing ReadOnly Mode on DataGridView Rows

c#winformsdatagridviewreadonly

提问by s????

Without explaining the entire context, my problem is basically this:

在不解释整个上下文的情况下,我的问题基本上是这样的:

I have a datagridview on a Windows Form which is bound to an Entity Framework DbSet: dbSet<TEntity>.Local.ToBindingList().

我在绑定到实体框架 DbSet: 的 Windows 窗体上有一个 datagridview dbSet<TEntity>.Local.ToBindingList()

If I set the datagridview's ReadOnlyproperty to true (in design view), and then have this statement in my code:

如果我将 datagridview 的ReadOnly属性设置为 true(在设计视图中),然后在我的代码中包含以下语句:

  myDataGridView.Rows[rowIndex].ReadOnly = false;

It steps right through without changing the value! (And no, my datasource is notreadonly.)

它直接通过而不改变值!(不,我的数据源不是只读的。)

Looping through the cells in the row and setting each cell's ReadOnly property individually doesn't work either:

循环遍历行中的单元格并单独设置每个单元格的 ReadOnly 属性也不起作用:

  foreach (DataGridViewCell cell in myDataGridView.Rows[rowIndex].Cells)
  {
      cell.ReadOnly = false;
  }

.ReadOnly- Gets or sets a value indicating whether the user can edit the cells of the DataGridView control.But it's acting like it can't setfor some reason.

.ReadOnly-Gets or sets a value indicating whether the user can edit the cells of the DataGridView control.但它表现得好像由于某种原因无法设置

Is there a reason that this would happen? I was thinking that maybe the datagridview's ReadOnlyproperty would override any changes made to specific rows/cells (ie. the whole formhas to be either readonly or not), but apparently thisworked for someone...

有什么理由会发生这种情况吗?我在想也许 datagridview 的ReadOnly属性会覆盖对特定行/单元格所做的任何更改(即整个表单必须是只读的或不是),但显然对某人有用......

There are other ways to accomplish my end-goal, but I would like to know why I can't change this property, as this would be the simplest solution for my situation.

还有其他方法可以实现我的最终目标,但我想知道为什么我不能改变这个属性,因为这将是我的情况最简单的解决方案。



EDIT:编辑:

Let me try to clarify my question:

让我试着澄清我的问题:

Again, I am aware that there are other ways to accomplish my end-goal, but would like an answer to this issue:

同样,我知道还有其他方法可以实现我的最终目标,但想得到这个问题的答案:

Sorry for the primitive example, but to easily replicate the issue, create a WinForm application, throw a datagridview on it, and past this code into your project:

对于原始示例很抱歉,但要轻松复制该问题,请创建一个 WinForm 应用程序,在其上抛出一个 datagridview,然后将此代码传递到您的项目中:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.ReadOnly = true;
        string[] row1 = new string[] { "test", "test1" };
        string[] row2 = new string[] { "test2", "test3" };
        string[] row3 = new string[] { "test4", "test5" };
        dataGridView1.Columns.Add("1", "1");
        dataGridView1.Columns.Add("2", "2");
        dataGridView1.Rows.Add(row1);
        dataGridView1.Rows.Add(row2);
        dataGridView1.Rows.Add(row3);

        dataGridView1.Rows[1].ReadOnly = false;
    }
}

If you put a breakpoint on the last line, and step through it, you will see that the ReadOnly property DOES NOT CHANGE! Why??

如果在最后一行放置一个断点并逐步执行,您将看到 ReadOnly 属性不会改变!为什么??

采纳答案by s????

So I have found a solution/workaround for my question, althought I am still unsure of the reasons WHY...

所以我为我的问题找到了解决方案/解决方法,尽管我仍然不确定为什么......

Apparently, if you want to change the ReadOnly property of a DataGridView row by row, you cannotset the mainDataGridView.ReadOnly property, as evidently this overrides any subsequent changes.

显然,如果要逐行更改 DataGridView 的 ReadOnly 属性,则不能设置DataGridView.ReadOnly 属性,因为这显然会覆盖任何后续更改。

To reuse my previous example, the workaround would be to loop through the rows and set each ROW'sReadOnly property (as opposed to setting the datagridview'sReadOnly property), THEN you can change each row's ReadOnly property individually:

要重用我之前的示例,解决方法是遍历行并设置每个ROW 的ReadOnly 属性(与设置datagridview 的ReadOnly 属性相反),然后您可以单独更改每行的 ReadOnly 属性:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //dataGridView1.ReadOnly = true;
        string[] row1 = new string[] { "test", "test1" };
        string[] row2 = new string[] { "test2", "test3" };
        string[] row3 = new string[] { "test4", "test5" };
        dataGridView1.Columns.Add("1", "1");
        dataGridView1.Columns.Add("2", "2");
        dataGridView1.Rows.Add(row1);
        dataGridView1.Rows.Add(row2);
        dataGridView1.Rows.Add(row3);

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            row.ReadOnly = true;
        }

        dataGridView1.Rows[1].ReadOnly = false;
    }
}

This is working great for me, however any insight as to why the code in my original question does not work correctly would be appreciated!

这对我来说很有用,但是任何有关为什么我的原始问题中的代码无法正常工作的见解将不胜感激!