免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
c# – 在編輯模式下更改datagridview單元格值

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

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

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

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

要抓住編輯的開(kāi)始,請(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;    }}
來(lái)源:https://www.icode9.com/content-1-276601.html
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
c# winform獲取gridview數(shù)據(jù)
DataGridView DataGridViewCheckBoxColumn編輯時(shí)實(shí)時(shí)觸發(fā)事件
C#實(shí)例:datagridview單元格合并
Windows Forms DataGridView 中合并單元格(轉(zhuǎn))
真有這個(gè)功能,Winform DataGridView仿Excel下拉復(fù)制
DataGridView 添加checkbox
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服