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

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

本文介绍了以编程方式更改DataGridView行上的只读模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

  myDataGridView.Rows[rowIndex].ReadOnly = false;

它可以直接执行而不更改值! (而且,我的数据源不是 not 只读。)

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

在行中的单元格中循环并分别设置每个单元格的ReadOnly属性不会工作之一:

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

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

-获取或设置一个值,该值指示用户是否可以编辑DataGridView控件的单元格。 / code>但这是出于某种原因而无法设置的行为。

是否有发生这种情况的原因?我在想,也许datagridview的 ReadOnly 属性会覆盖对特定行/单元格所做的任何更改(即完整格式必须为只读)或没有),但显然为某人工作...

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...

还有其他方法可以完成我的最终目标,但我想知道为什么我不能更改此属性,因为这是最简单的方法解决方案。

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.



编辑:

让我尝试澄清我的问题:

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:

很抱歉,原始示例很简单,但是为了轻松复制问题,请创建一个WinForm应用程序,在其上放置一个datagridview,然后将此代码传递到您的项目中:

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:

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;
    }
}

如果在最后一行放置断点,并且单步执行操作,您将看到ReadOnly属性没有变化!为什么??

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

推荐答案

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

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

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

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.

要重用我之前的示例,解决方法是遍历行并设置每个 ROW的 ReadOnly属性(而不是设置 datagridview的 ReadOnly属性),那么您可以分别更改每一行的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:

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!

这篇关于以编程方式更改DataGridView行上的只读模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:16