在编辑模式下更改datagridview单元格值 [英] Change datagridview cell value in edit mode

查看:146
本文介绍了在编辑模式下更改datagridview单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在datagridview中有一个单元格,其中以自定义格式显示时间。我需要在使用时进入编辑模式(例如通过双击),我需要将字符串值更改为代表以分钟为单位的时间的整数。

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.

更改 CellEnter事件中的单元格值,它似乎没有响应。实际上,在任何事件中似乎都不会改变单元格的值。

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

然后感谢TaW,我在CellBeginEdit上展示了对其进行编辑的原始值:

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

最后,当CellEndEdit时,我重新格式化新值:

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


推荐答案

何时单元格处于编辑模式,您需要在编辑控件(通常是文本框)中更改文本。您可以在 EditingControlShowing 事件中获取(并按住)它的句柄:

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

但使用 CellEnter 事件不是一个好主意,因为滚动或单击时也会调用它。.

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

要开始编辑,请使用 BeginEdit 事件:

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

这篇关于在编辑模式下更改datagridview单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆