I have a cell in datagridview in which I display time in a custom format. I need when used enters edit mode (for example by double-click), I need to change the string value to integer representing the time in minutes.
When I try to change the cell value in "CellEnter" event, it doesn't seem to respond. Actually it doesn't seem to change the cell value pretty much inside any event.
Please don't mind the details of converting time to string and vise versa, my question is how can I successfully change the content of a cell when user double-clicks on it.
Edit (code + solution): What I did is use another column to store the actual value (without formatting). On cell formatting of that column I'm passing the value to custom format function to fill my column.
private void gridview_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
if (e.ColumnIndex == 3 && e.Value != null && e.Value.ToString() != "")
{
//fill the unbound textbox column (5) from raw value column (3)
string newValue = TimeAttendanceHelper.FormatHourlyDuration(e.Value);
gridview.Rows[e.RowIndex].Cells[5].Value = newValue;
}
}
And then thanks to TaW, on CellBeginEdit I am showing the raw value to edit it:
private void gridview_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if (e.ColumnIndex == 5)
{
//on editing, use the value from raw data column (3)
gridview.Rows[e.RowIndex].Cells[5].Value = gridview.Rows[e.RowIndex].Cells[3].Value;
}
}
And Finally when CellEndEdit, I reformat the new value:
private void gridview_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 4)
{
//update value both in columns 3 & 5
string newValue = tIME_SHIFTDataGridView.Rows[e.RowIndex].Cells[4].Value.ToString();
gridview.Rows[e.RowIndex].Cells[3].Value = newValue;
gridview.Rows[e.RowIndex].Cells[4].Value = TimeAttendanceHelper.FormatHourlyDuration(newValue);
}
}