简体   繁体   English

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

[英]Programmatically Changing ReadOnly Mode on DataGridView Rows

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 ReadOnly property 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 not readonly.) (不,我的数据源不是只读的。)

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. .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 set for some reason.但它表现得好像由于某种原因无法设置

Is there a reason that this would happen?有什么理由会发生这种情况吗? I was thinking that maybe the datagridview's ReadOnly property would override any changes made to specific rows/cells (ie. the whole form has to be either readonly or not), but apparently this worked 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!如果在最后一行放置一个断点并逐步执行,您将看到 ReadOnly 属性不会改变! Why??为什么??

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 cannot set the main DataGridView.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's ReadOnly property (as opposed to setting the datagridview's ReadOnly 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!这对我来说很有用,但是任何有关为什么我的原始问题中的代码无法正常工作的见解将不胜感激!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM