[SOLVED] DGV Cell tools event handler - can't change cell.Value

Answered Closed
0
0

On my DGV, I have a Tool in a TextBox column. The tool has a proper name (tool.Name = “clear”).

The event handler fetches the cell

var cell = ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex];

and set its value to empty

cell.Value = string.Empty;

The debugger shows that each step does what it’s supposed to do but the fact is the DGV cell shows the same content (if I set it to “Abc”, after running the tool it shows “Abc” all the same)

Sample attached

Attachment
  • You must to post comments
Best Answer
0
0

Hi Tiago,

It changes the value of the cell, but you don’t see it because the editor is in front of it. Try to change the editor:

((DataGridView)sender).EditingControl.Text = “”;

It’s the same in WinForms. Cell.Value is always the stored value. Cell.EditedFormattedValue is the value in the editor – if in edit mode – or the cell, but it’s read-only.

Best,

Luca

  • You must to post comments
0
0

Changed the handler to

private void dataGridView1_CellToolClick(object sender, DataGridViewToolClickEventArgs e)
{
    var cell = ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex];
    cell.Value = string.Empty;
 
    ((DataGridView)sender).EditingControl.Text = cell.Value.ToString();
 
}

and now it works.

  • You must to post comments
Showing 2 results