小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

c# – 在編輯模式下更改datagridview單元格值

 印度阿三17 2019-06-28

我在datagridview中有一個單元格,我以自定義格式顯示時間.我需要在使用時進入編輯模式(例如通過雙擊),我需要將字符串值更改為整數(shù),表示以分鐘為單位的時間.

當我嘗試更改“CellEnter”事件中的單元格值時,它似乎沒有響應.實際上它似乎并沒有在任何事件中改變單元格值.

請不要介意將時間轉換為字符串的細節(jié),反之亦然,我的問題是如何在用戶雙擊時成功更改單元格的內容.

編輯(代碼解決方案):
我所做的是使用另一列來存儲實際值(沒有格式化).在該列的單元格格式上,我將值傳遞給自定義格式函數(shù)以填充我的列.

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上我顯示了編輯它的原始值:

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時,我重新格式化了新值:

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事件中獲取(并保持)它的句柄:

TextBox editBox = null;

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

但是使用CellEnter事件并不是一個好主意,因為在滾動或點擊時也會調用它.

要抓住編輯的開始,請使用BeginEdit事件:

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;
    }
}
來源:https://www./content-1-276601.html

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多