Why can't I edit the values in my DataGridView, even though its not set to ReadOnly?
Asked Answered
I

8

10

I have a DataGridView, which is not set to ReadOnly. None of its columns are set to ReadOnly, and the object it is bound to is not set to ReadOnly. Yet, I can't edit the DataGridView items? The .DataSource property of the DataGridView is set to a ReadOnlyCollection<>, but I can programmatically alter the elements, just not from the UI. What's going on?

Impignorate answered 27/5, 2009 at 12:23 Comment(1)
How are you trying to edit the elements? What are the behaviors that you think you should be seeing that you don't see?Oldie
I
14

It turns out that if your DataGridView is bound to a ReadOnlyCollection, then even though you can programatically edit any item in the collection, the DataGridView will restrict you from changing the values. I'm not sure if this behavior is intentional, but its something to watch out for.

Impignorate answered 27/5, 2009 at 12:24 Comment(3)
My bad, it turns out that "the one column that can be changed" was actually not correctly bound to the datasource all along.Impignorate
+1 I wish I had searched SO in the morning. This one took me hours of banging my head against the wall before I figured it out.Sabaean
Also make sure the EditMode on your gridview is set to "EditOnKeystrokeOrF2"Cancellation
D
5

I installed VS 2013 just yesterday, latest build (update 5) and a bug still remains that causes the behavior you describe.

In short to work around the bug, first make sure the datagridview is set to be GUI-editable. This especially includes the tiny arrow in the form designer at the top right of the control. In the arrow drop down is an option "enable editing", ensure it's enabled. Now in the form designer edit the columns in some major manner (such as add or remove a column). That's it, when you run the program you should find the GUI editing is working now.

To reproduce this bug, within the form designer use the tiny arrow at the top right of the datagridview control to set "enable editing" to false. Now make a major change to the columns (such as add or remove a column). Compile and run the program. Now go back to the tiny arrow and re-enable "enable editing" checkbox. Again run the program. At this point the bug manifests itself, and you will find that the datagridview is not GUI-editable even though you've configured otherwise in the VS.

Diplo answered 15/6, 2016 at 13:16 Comment(0)
R
4

This is just an extended comment (hence wiki) in counter to the "the DataGridView will restrict you from changing some values (strings) but not other values (bools)" point; neither is editable; make it a List<T> and both are editable...:

using System;
using System.Collections.ObjectModel;
using System.Windows.Forms;
class Test
{
    public string Foo { get; set; }
    public bool Bar { get; set; }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        var data = new ReadOnlyCollection<Test>(
            new[] {
                new Test {Foo="abc", Bar=true},
                new Test {Foo="def", Bar=false},
                new Test {Foo="ghi", Bar=true},
                new Test {Foo="jkl", Bar=false},
            });
        Application.Run(
            new Form {
                Text = "ReadOnlyCollection test",
                Controls = {
                    new DataGridView {
                        Dock = DockStyle.Fill,
                        DataSource = data,
                        ReadOnly = false
                    }
                }
            });
    }
}
Reckless answered 27/5, 2009 at 12:23 Comment(1)
Whoops. Turns out that the "one editable column" was actually a typo in the databinding code, so it was actually "the one editable column that was not actually bound to the data source the entire time.".Impignorate
B
3

How are you binding to your DataGridView? One thing is that if you are using a Linq list as the datasource queried from a database and you do not have the complete object, then the properties are readonly unless you specify "with new" in the select function. There is not a lot of information in your post. I hope this helps.

Bookerbookie answered 27/5, 2009 at 12:52 Comment(0)
L
1

If you have bound the grid to a collection which has been defined globally , try to create a local clone copy of the collection in the spot(function) you are binding in and bind the grid to the new collection. That worked for me.

Louque answered 15/9, 2020 at 12:28 Comment(0)
R
1

I put 2-5 hours searching on it, Just make VirtualMode property of datagridview false, now we can set data to datagridview cells programmatically.

Ronaldronalda answered 19/8, 2024 at 21:22 Comment(0)
B
0

Nothing worked for me. I didn't use binding. Just made everything NOT read only and set edit mode to (tried all values). The cells were always read-only.

The way I made it work was to set the cell to ReadOnly = false in the event handler:

private void gridViewSettings_CellClick(object sender,dataGridViewCellEventArgs e)
{
        gridViewSettings.CurrentCell = gridViewSettings.Rows[e.RowIndex].Cells[e.ColumnIndex];
        gridViewSettings.CurrentCell.ReadOnly = false;
        gridViewSettings.BeginEdit(true);
}

At the end of this handler, the cell is in edit mode (the cell I clicked on). Made the changes in the GUI and the rest (if any) is handled in the CellEndEdit event handler. This handler is called when the mouse goes out of cell or you press Enter.
At this point the new value is saved in CurrentCell.Value;

String newValue = (String)gridViewSettings.CurrentCell.Value;
Beekeeping answered 6/6, 2021 at 8:38 Comment(0)
A
-2

Take datagridview task and click on edit column then set the column Read Only Property to False

Arietta answered 11/2, 2016 at 12:31 Comment(1)
Please read the title of this question,its says that he already did that.Pearlpearla

© 2022 - 2025 — McMap. All rights reserved.