1

I have a DataGridView I'm trying to edit manually.

-grid.ReadOnly is false
-all Columns ReadOnly are false
-EditMode is OnKeyStroke
-not bound to anything
-no data validation

If I enter a value in the grid and then press enter or click onto another cell, the entered value disappears.

I have the following events setup on the DataGridView:
CellMouseDoubleClick

private void trackEditor_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridView grid = sender as DataGridView;
            //checks if the cell's value contains a 0. If so, change the value to write to 1. If not, assume the cell value is already 0 and keep the value to write to 0
            int val;
            if (grid.CurrentCell.Value == null || int.Parse(grid.CurrentCell.Value.ToString()) == 0)
                val = 1;
            else 
                val = 0;
            grid.CurrentCell.Value = val;
        }

CellValueChanged

private void trackEditor_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            string _out = "";
            for (int x = 0; x < trackEditor.ColumnCount; x++) {
                if (trackEditor.CurrentRow.Cells[x].Value != null)
                    _out += x + ":" + trackEditor.CurrentRow.Cells[x].Value + ",";
            }
            richRawTrackData.Text = _out;
        }
CocoaMix86
  • 69
  • 1
  • 9
  • What is it bound to? Are you using validation? Any other events subscribed to? Please [edit] into your post any code you think relevant – Charlieface Jan 15 '21 at 03:03
  • Updated post @Charlieface – CocoaMix86 Jan 15 '21 at 03:07
  • Is this for every column or only certain ones? `column.ValueType` and `FormattedValueType` are what? Any other odd settings? – Charlieface Jan 15 '21 at 03:16
  • Every column and cell appears to be affected. `ValueType` is string. I don't have `FormattedValueType` set – CocoaMix86 Jan 15 '21 at 03:20
  • You don't see the edited value in `richRawTrackData.Text` ? – Chetan Jan 15 '21 at 03:21
  • No I don't. `CellValueChanged` is being triggered when I type in the cells and exit them, so I can verify that much. – CocoaMix86 Jan 15 '21 at 03:28
  • I am unable to reproduce what you describe. I do not see anything in the code that would cause what you describe. The only issue I see is that the code will crash in the `CellMouseDoubleClick` event if the `CurrentCells` value is not a valid `int` value. At no time did I type a value into a cell and then have the cell become “empty” after leaving the cell. Are you sure there are no other events wired up? – JohnG Jan 15 '21 at 03:50
  • It might have to do something with the fact that I'm dynamically changing the size of the table? I have buttons on the form to add/remove rows and columns. The grid starts with no rows and 255 columns when initialized. – CocoaMix86 Jan 15 '21 at 03:57
  • Without seeing the code, it is difficult to say. As far as deducing “where” the problem lies, I can confirm that the posted code would not produce what you describe, so I am confident something else is causing the text to disappear. When you say… _”changing the size of the table”_ … do you mean the size of the grid? Is there a specific “time” when this happens (text disappearing) or does it happen in all cells all the time? – JohnG Jan 15 '21 at 04:14
  • @JohnG it happens all of the time, any time I try to edit a cell. When I change the size of the table, I am calling `grid.RowCount` and `grid.ColumnCount`. – CocoaMix86 Jan 15 '21 at 04:21
  • What triggers the re-sizing of the grid and how is this done? Can you show this code? It would appear clear that if you have other events wired up to the grid, that one of them is causing this… Disable all of the events and add them one at a time to see which one is the culprit. I feel confident you can eliminate the two posted events; however, it may well be a combination of events. In other words, to help, we would need to see the code that reproduces the problem. Currently the posted code fails this requirement. – JohnG Jan 15 '21 at 04:31
  • I suggest you create a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) … fortunately creating the MRE will usually reveal the problem. – JohnG Jan 15 '21 at 04:32
  • I don't know what is the real problem with your code but there is already a verified answer in stackoverflow for "how to edit datagridview" .Please check the link: https://stackoverflow.com/questions/1814423/datagridview-how-to-set-a-cell-in-editing-mode – Srijon Chakraborty Jan 15 '21 at 04:44

1 Answers1

0

I found the answer. By creating a duplicate DGV and comparing Properties one by one, I found that it was the VirtualMode property that prevented entering data. Setting it to false allowed data to be entered now.

CocoaMix86
  • 69
  • 1
  • 9