久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

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

 印度阿三17 2019-06-28

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

當(dāng)我嘗試更改“CellEnter”事件中的單元格值時(shí),它似乎沒有響應(yīng).實(shí)際上它似乎并沒有在任何事件中改變單元格值.

請(qǐng)不要介意將時(shí)間轉(zhuǎn)換為字符串的細(xì)節(jié),反之亦然,我的問題是如何在用戶雙擊時(shí)成功更改單元格的內(nèi)容.

編輯(代碼解決方案):
我所做的是使用另一列來存儲(chǔ)實(shí)際值(沒有格式化).在該列的單元格格式上,我將值傳遞給自定義格式函數(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;
    }
}

最后,當(dāng)CellEndEdit時(shí),我重新格式化了新值:

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

解決方法:

當(dāng)單元格處于編輯模式時(shí),您需要更改編輯控件中的文本,通常是文本框.您可以在EditingControlShowing事件中獲取(并保持)它的句柄:

TextBox editBox = null;

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

但是使用CellEnter事件并不是一個(gè)好主意,因?yàn)樵跐L動(dòng)或點(diǎn)擊時(shí)也會(huì)調(diào)用它.

要抓住編輯的開始,請(qǐng)使用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

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

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多