简体   繁体   中英

Change datagridview cell value in edit mode

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);
    }
}

When the cell is in edit mode you need to change the text in the edit control, usually a Textbox. You can get (and hold) a handle to it in the EditingControlShowing event:

TextBox editBox = null;

private void dataGridView1_EditingControlShowing(object sender,
                           DataGridViewEditingControlShowingEventArgs e)
{
    if (e.Control is TextBox) editBox = e.Control as TextBox;
}

But using the CellEnter event is not a good idea as it will be called when scrolling or clicking around as well..

To catch the beginning of editing you use the BeginEdit event:

int yourEditColumn = 5;

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
    if (e.ColumnIndex == yourEditColumn )
    {
        string yourValue = "12345";
        dataGridView1.Rows[e.RowIndex].Cells[yourEditColumn ].Value = yourValue;
        if (editBox != null)   editBox.Text = yourValue;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM