I have a cell in a datagridview in which I display the time in a custom format. I need it when it enters edit mode (for example, by double-clicking), I need to change the string value to an integer representing the time in minutes.
When I try to change the value of a cell in the "CellEnter" event, it does not seem to respond. Actually, it doesn't seem to change the value of the cell in any way.
Please ignore the details of converting time to string and vice versa, my question is how can I successfully change the contents of a cell when the user double-clicks on it.
Edit (code + solution): I used a different column to store the actual value (without formatting). When formatting a cell in this column, I pass the value to a custom format function to populate my column.
private void gridview_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { if (e.ColumnIndex == 3 && e.Value != null && e.Value.ToString() != "") {
And then thanks to TaW, on CellBeginEdit, I show the original value for editing it:
private void gridview_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { if (e.ColumnIndex == 5) {
And finally, when CellEndEdit, I reformat the new value:
private void gridview_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 4) {
source share