[英]DataGridView get pre-changed cell value
我試圖在更改之前立即獲取單元格的值。 因此,當我進行.Text
更改時,我想在進行文本更改之前獲取更改前的值。 是否有一個事件可以觸發我獲取此數據?
我嘗試使用BeginCellEdit,CellValidating等...,但是這些都沒有觸發,當值更新時,它們甚至都沒有在方法調用上達到我的斷點。
我寧願不必在更改期間手動刪除很多get .Texts
,而只希望一個事件獲得預先更改的值。 是否有其他方法或方法來獲取預先更改的單元格值?
如何創建一種無需重復代碼的方法?
而不是這樣:
frameGridView.Rows[frameNumber].Cells[1].Value = subArray[0];
在實際更新單元格之前,創建一個使用舊值執行所需操作的方法:
private void UpdateGrid(int rowIndex, int columnIndex, string newValue)
{
var cell = frameGridView.Rows[frameNumber].Cells[1];
var oldValue = Convert.ToString(cell.Value);
// display oldValue in the section that shows recent changes
cell.Value = newValue; // update the cell
}
然后同樣地稱呼它:
UpdateGrid(frameNumber, 1, subArray[0]);
希望對您有所幫助:
private string oldValue;
private bool isValid;
void myDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
this.isValid= true;
this.oldValue= Convert.ToString(this[e.ColumnIndex, e.RowIndex].Value);
if (!isValid(this.myDataGridView(e))
isValid= false;
}
void myDataGridView_CellValidated(object sender, DataGridViewCellValidatingEventArgs e)
{
if (!isValid)
this[e.ColumnIndex, e.RowIndex].Value = this.oldValue;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.