我試圖將結構的表綁定到DataGridView。加載和查看錶格工作正常,但我無法編輯值並將其存回表格中。這是我正在做的。綁定DataGridView不更新數據源
我有一個「原始」數據類型,通過
public struct MyReal:IMyPrimative
{
public Double m_Real;
//...
public MyReal(String val)
{
m_Real = default(Double);
Init(val);
}
//...
}
真正定義它得到在結構中使用:
public struct MyReal_Record : IMyRecord
{
public MyReal Freq { get; set;}
MyReal_Record(String[] vals)
{
Init(vals);
}
}
並且該結構被用於利用一個通用的綁定定義一個表列表
public class MyTable<S> : BindingList<S> where S: struct, IMyRecord
{
public Type typeofS;
public MyTable()
{
typeofS = typeof(S);
// ...
}
該表格被動態地用作網格的綁定源。
private void miLoadFile_Click(object sender, EventArgs e)
{
MyModel.Table<Real_Record> RTable = new MyModel.Table<Real_Record>();
//... Table initialized here
//set up grid with virtual mode
dataGridView1.DataSource = RTable;
}
所有這些工作正常,我可以創建RTable,初始化它並將其顯示在網格中。該網格允許編輯並具有CellParsing設置事件和CellFormatting看起來像:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.DesiredType != typeof(String))
return;
e.Value = e.Value.ToString();
}
private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
if (e.DesiredType != typeof(MyReal))
return;
e.Value = new MyReal(e.Value.ToString());
e.ParsingApplied = true;
this.dataGridView1.UpdateCellValue(e.ColumnIndex, e.RowIndex);
}
當我在一個單元格編輯值,我可以改變文本。在離開單元格時,CellParsing觸發並調用事件處理程序。一切似乎都正確進入CellParsing處理程序。 e.DesiredType是MyReal。 e.Value是具有新值的字符串。從字符串中創建新的MyReal後,e.Value設置正確。 RowIndex和ColumnIndex是正確的。 ReadOnly設置爲false。
但是,當我離開單元格時,系統會將原始值恢復到單元格。我認爲UpdateCellValue會替換dataSource中的值,但我似乎錯過了一些東西。
我錯過了什麼?
感謝, 最大